CommonController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: zilongs
  5. * Date: 20-9-23
  6. * Time: 上午11:26
  7. */
  8. namespace App\Http\Controllers\Api\V1;
  9. use App\Http\Controllers\Controller;
  10. use App\Models\Area;
  11. use App\Models\HelpInfo;
  12. use App\Models\Icons;
  13. use App\Models\Order;
  14. use App\Models\OrderPatient;
  15. use App\Models\User;
  16. use EasyWeChat\Factory;
  17. use Cache;
  18. class CommonController extends Controller
  19. {
  20. public function wxLogin()
  21. {
  22. $req = request()->post();
  23. if (!empty($req['test']) && $req['test'] == 'test') {
  24. print_r(send_wechat_message(12, ['o481Y5KqunsAxzcCfGNImch-eZrk', 11,22,33,44,55,66,77], []));die;
  25. }
  26. $this->validate(request(), [
  27. 'wechat_code' => 'required',
  28. 'iv' => 'required',
  29. 'encryptData' => 'required',
  30. 'phone|手机号' => 'max:20',
  31. 'verify_code|验证码' => 'integer',
  32. 'nickname|昵称' => 'max:50',
  33. 'avatar|头像' => 'url',
  34. 'latitude|纬度' => 'numeric',
  35. 'longitude|纬度' => 'numeric',
  36. ]);
  37. if (!empty($req['phone'])) {
  38. if (empty($req['verify_code'])) {
  39. return out(null, 10003, '验证码不能为空');
  40. }
  41. $verify_code = Cache::get($req['phone'].'-1');
  42. if($verify_code != $req['verify_code']){
  43. if (env('APP_ENV') == 'online' || $req['verify_code'] != '111111') {
  44. return out(null, 10004, '验证码错误');
  45. }
  46. }
  47. Cache::delete($req['phone'].'-1');
  48. }
  49. $app = Factory::miniProgram(config('config.wechat_small_program'));
  50. $data = $app->auth->session($req['wechat_code']);
  51. if (empty($data['openid'])){
  52. return out(null, 10001, '微信登录code错误');
  53. }
  54. $session_key = !empty($data['session_key']) ? $data['session_key'] : '';
  55. $user = User::select(['id', 'status', 'phone', 'nickname', 'avatar'])->where('openid', $data['openid'])->first();
  56. if(!empty($req['iv']) && !empty($req['encryptData'])) {
  57. $decryptedData = $app->encryptor->decryptData($session_key, $req['iv'], $req['encryptData']);
  58. \Log::info($decryptedData);
  59. if(isset($decryptedData['unionId']))$data['unionid'] = $decryptedData['unionId'];
  60. }
  61. if (empty($user)){
  62. $add = [
  63. 'openid' => $data['openid'],
  64. 'unionid' => $data['unionid'] ?? '',
  65. 'latitude' => $req['latitude'] ?? 0,
  66. 'longitude' => $req['longitude'] ?? 0,
  67. 'phone' => $req['phone'] ?? '',
  68. 'session_key' => $session_key,
  69. 'last_login_time' => time(),
  70. ];
  71. $add['nickname'] = $req['nickname'] ?? '';
  72. $add['avatar'] = $req['avatar'] ?? request()->getScheme().'://'.request()->getHost().'/img/default-head.png';
  73. $user = User::create($add);
  74. if (empty($add['nickname'])) {
  75. $nickname = '用户'.base_convert($user['id'], 10, 16);
  76. User::where('id', $user['id'])->update(['nickname' => $nickname]);
  77. }
  78. }
  79. else {
  80. if ($user['status'] == 0) {
  81. return out(null, 10002, '该账号已被冻结');
  82. }
  83. $update = [
  84. 'nickname' => $req['nickname'] ?? '',
  85. 'unionid' => $data['unionid'] ?? '',
  86. 'avatar' => $req['avatar'] ?? '',
  87. 'latitude' => $req['latitude'] ?? 0,
  88. 'longitude' => $req['longitude'] ?? 0,
  89. 'phone' => $req['phone'] ?? '',
  90. 'session_key' => $session_key,
  91. 'last_login_time' => time(),
  92. ];
  93. $update = array_filter($update);
  94. if (empty($req['nickname']) && empty($user['nickname'])) {
  95. $update['nickname'] = $req['nickname'] = '用户'.base_convert($user['id'], 10, 16);;
  96. }
  97. if (empty($req['avatar']) && empty($user['avatar'])) {
  98. $update['avatar'] = $req['avatar'] = request()->getScheme().'://'.request()->getHost().'/img/default-head.png';
  99. }
  100. User::where('id', $user['id'])->update($update);
  101. }
  102. $token = aes_encrypt(['id' => $user['id'], 'time' => time()]);
  103. $req['token'] = $token;
  104. $req['id'] = $user['id'];
  105. $req['flag'] = 'member_'.$user['id'];
  106. return out($req);
  107. }
  108. public function getPhoneNumber()
  109. {
  110. $req = request()->post();
  111. $this->validate(request(), [
  112. 'wechat_code' => 'required',
  113. 'iv' => 'required',
  114. 'encryptData' => 'required',
  115. ]);
  116. try {
  117. $app = Factory::miniProgram(config('config.wechat_small_program'));
  118. $data = $app->auth->session($req['wechat_code']);
  119. if (empty($data['openid']) || empty($data['session_key'])){
  120. return out(null, 10001, '微信code错误');
  121. }
  122. $decryptedData = $app->encryptor->decryptData($data['session_key'], $req['iv'], $req['encryptData']);
  123. if (empty($decryptedData['phoneNumber'])) {
  124. return out(null, 10002, '授权手机号错误');
  125. }
  126. \Log::info($decryptedData);
  127. if(isset($decryptedData['unionId']))$data['unionid'] = $decryptedData['unionId'];
  128. if (!User::where('openid', $data['openid'])->exists()){
  129. $user = User::create([
  130. 'openid' => $data['openid'],
  131. 'unionid' => $data['unionid']?? '',
  132. 'phone' => $decryptedData['phoneNumber'],
  133. 'session_key' => $data['session_key'],
  134. 'avatar' => request()->getScheme().'://'.request()->getHost().'/img/default-head.png',
  135. 'last_login_time' => time(),
  136. ]);
  137. $nickname = '用户'.base_convert($user['id'], 10, 16);
  138. User::where('id', $user['id'])->update(['nickname' => $nickname]);
  139. }
  140. } catch (\Exception $e) {
  141. return out(null, 10003, '获取手机号失败', $e->getMessage());
  142. }
  143. return out($decryptedData);
  144. }
  145. public function getUserByCode()
  146. {
  147. $req = request()->post();
  148. $this->validate(request(), [
  149. 'wechat_code' => 'required',
  150. ]);
  151. $app = Factory::miniProgram(config('config.wechat_small_program'));
  152. $data = $app->auth->session($req['wechat_code']);
  153. if (empty($data['openid'])){
  154. return out(null, 10001, '微信code错误');
  155. }
  156. $user = User::select(['id', 'status', 'phone', 'nickname', 'avatar'])->where('openid', $data['openid'])->first();
  157. if (!empty($user) && $user['status'] == 0) {
  158. return out(null, 10002, '该账号已被冻结');
  159. }
  160. return out($user);
  161. }
  162. public function uploadFile()
  163. {
  164. $file = request()->file('file');
  165. if (empty($file)) {
  166. return out(null, 10001, '文件不能为空');
  167. }
  168. $path = $file->store('uploads/'.date('Ymd'));
  169. $url = request()->getScheme().'://'.request()->getHost().'/'.$path;
  170. return out(['url' => $url]);
  171. }
  172. public function areaList()
  173. {
  174. $areas = Area::get();
  175. $data1 = $data2 = $data3 = [];
  176. foreach ($areas as $k => $v) {
  177. if ($v['level'] == 1) {
  178. $data1[$k]['id'] = $v['id'];
  179. $data1[$k]['name'] = $v['name'];
  180. $data1[$k]['sub'] = [];
  181. }
  182. if ($v['level'] == 2) {
  183. $arr = [];
  184. $arr['id'] = $v['id'];
  185. $arr['name'] = $v['name'];
  186. //$arr['sub'] = [];
  187. $data2[$v['parent_id']][] = $arr;
  188. }
  189. /*if ($v['level'] == 3) {
  190. $arr = [];
  191. $arr['id'] = $v['id'];
  192. $arr['name'] = $v['name'];
  193. $data3[$v['parent_id']][] = $arr;
  194. }*/
  195. }
  196. /*foreach ($data2 as $k => $v) {
  197. foreach ($v as $k1 => $v1) {
  198. $data2[$k][$k1]['sub'] = $data3[$v1['id']]??[];
  199. }
  200. }*/
  201. foreach ($data1 as $k => $v) {
  202. $data1[$k]['sub'] = $data2[$v['id']]??[];
  203. }
  204. return out($data1);
  205. }
  206. public function sendVerifyCode()
  207. {
  208. //防止恶意刷验证码接口,一分钟最多10次
  209. check_repeat_request(60, 10);
  210. $req = request()->post();
  211. $this->validate(request(), [
  212. 'type' => 'required|integer',
  213. 'phone|手机号' => 'required',
  214. ]);
  215. if($req['type'] == 3 || $req['type'] == 4){
  216. $user = User::getUserByToken();
  217. if ($user['phone'] != $req['phone']){
  218. return out(null, 30007, '该手机号不是注册时的手机号');
  219. }
  220. }
  221. if ($req['type'] == 5) {
  222. if (User::where('phone', $req['phone'])->exists()) {
  223. return out(null, 30008, '此手机号已经被占用');
  224. }
  225. }
  226. $verify_code = generate_code();
  227. $result = send_sms($req['phone'], 'verify_template_code', ['code' => $verify_code]);
  228. if (empty($result['Code']) || $result['Code'] != 'OK'){
  229. return out(null, 30010, '验证码发送失败,请稍后重试');
  230. }
  231. Cache::set($req['phone'].'-'.$req['type'], $verify_code, config('config.aly_sms.sms_verify_code_expire'));
  232. return out();
  233. }
  234. public function doc()
  235. {
  236. $database = env('DB_DATABASE');
  237. $prefix = env('DB_PREFIX');
  238. $exclude_tables = "'bm_password_resets','bm_admin_menu','bm_admin_users','bm_failed_jobs','bm_migrations'";
  239. $sql = "select TABLE_NAME name,TABLE_COMMENT comment from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA='".$database."' and TABLE_NAME not in (".$exclude_tables.")";
  240. $tables = \DB::select($sql);
  241. $map1 = $map2 = [];
  242. $i = round(count($tables)/2);
  243. foreach ($tables as $k => $v) {
  244. $name = str_replace($prefix, '', $v->name);
  245. if ($k >= $i) {
  246. $map1[$v->name] = $name.'('.$v->comment.')';
  247. }
  248. else {
  249. $map2[$v->name] = $name.'('.$v->comment.')';
  250. }
  251. }
  252. $data1 = [];
  253. foreach ($map1 as $k => $v){
  254. $sql = "select COLUMN_NAME name, DATA_TYPE type, COLUMN_COMMENT comment from INFORMATION_SCHEMA.COLUMNS where table_schema = '".$database."' AND table_name = '".$k."'";
  255. $comment = \DB::select($sql);
  256. $data1[$v] = $comment;
  257. }
  258. $data2 = [];
  259. foreach ($map2 as $k => $v){
  260. $sql = "select COLUMN_NAME name, DATA_TYPE type, COLUMN_COMMENT comment from INFORMATION_SCHEMA.COLUMNS where table_schema = '".$database."' AND table_name = '".$k."'";
  261. $comment = \DB::select($sql);
  262. $data2[$v] = $comment;
  263. }
  264. return view('doc', ['data1' => $data1, 'data2' => $data2]);
  265. }
  266. public function getHelpList()
  267. {
  268. $data = HelpInfo::where('status', 1)->orderBy('id', 'desc')->paginate();
  269. return out($data);
  270. }
  271. public function configList()
  272. {
  273. $data = [
  274. 'banners' => [
  275. 'https://t5.9026.com/img/banner1.png',
  276. 'https://t5.9026.com/img/banner2.png',
  277. 'https://t5.9026.com/img/banner3.png',
  278. ],
  279. 'notices' => [
  280. '宝妈小程序正式公测上线啦!',
  281. ],
  282. ];
  283. return out($data);
  284. }
  285. public function getCity()
  286. {
  287. $req = request()->post();
  288. $this->validate(request(), [
  289. 'name' => 'required'
  290. ]);
  291. $area = Area::where('name', 'like', $req['name'].'%')->first();
  292. if (!empty($area) && $area['level'] == 3) {
  293. $area = Area::where('id', $area['parent_id'])->first();
  294. }
  295. return out($area);
  296. }
  297. public function buildToken()
  298. {
  299. $req = request()->post();
  300. $this->validate(request(), [
  301. 'user_id' => 'integer',
  302. 'docter_id' => 'integer',
  303. ]);
  304. $token = '';
  305. if (!empty($req['user_id'])) {
  306. $token = aes_encrypt(['id' => $req['user_id'], 'time' => time()]);
  307. }
  308. elseif (!empty($req['docter_id'])) {
  309. $token = aes_encrypt(['docter_id' => $req['docter_id'], 'time' => time()]);
  310. }
  311. return out(['token' => $token]);
  312. }
  313. public function delServerFile()
  314. {
  315. $req = request()->post();
  316. $this->validate(request(), [
  317. 'url' => 'required|url'
  318. ]);
  319. $tem = parse_url($req['url']);
  320. $allPath = public_path().$tem['path'];
  321. unlink($allPath);
  322. return out();
  323. }
  324. //下单通知
  325. public function subscribe()
  326. {
  327. $msgType = request('type',3); //1公众号 2医生端小程序 3 用户端小程序
  328. $orderId = request('order_id'); //订单id
  329. if(empty($orderId)){
  330. return out('',601,'请输入参数');
  331. }
  332. $config = [
  333. 1=>[
  334. 'app_id' => env('OFFICE_APPID', 'wx92066f7587c34617'),
  335. 'secret' => env('OFFICE_APPSECRET', 'bd7849c5259b79c41ec158173c5fb1d6'),
  336. 'response_type' => 'array'
  337. ],
  338. 2=>[
  339. 'app_id' => env('DOCTRER_WECHAT_APPID', 'wx92066f7587c34617'),
  340. 'secret' => env('DOCTRER_WECHAT_APPSECRET', 'bd7849c5259b79c41ec158173c5fb1d6'),
  341. 'response_type' => 'array'
  342. ],
  343. 3=>[
  344. 'app_id' => env('WECHAT_APPID', 'wx6131f74e623bf6bf'),
  345. 'secret' => env('WECHAT_APPSECRET', 'b703596ab6cbcad5b74eb51fec2aeb0b'),
  346. 'response_type' => 'array'
  347. ],
  348. ] ;
  349. $order = Order::where(['id'=>$orderId])->first();
  350. if(empty($order)) return out('',602,'无效订单');
  351. ;
  352. if(empty($order->user_id)) return out('',602,'无效订单');
  353. $user = \App\User::where('id',$order->user_id)->first();
  354. //获取unionid
  355. $uid = $user->unionid;
  356. if(empty($user)) return apiReturn(602,'此订单用户不存在');
  357. $name = OrderPatient::where(['order_id'=>$orderId])->value('name');
  358. $type_arr = [1=>'电话咨询',2=>'图文咨询',3=>'门诊预约',4=>'疫苗',5=>'儿保',6=>'服务包',7=>'充值'];
  359. $order = Order::where(['id'=>$orderId])->first();
  360. $orderType = $type_arr[$order->product_type];
  361. //如果没有unionid 则用 openid
  362. if(empty($user->unionid)) $uid = $user->openid;
  363. if($msgType ==1){
  364. $app = Factory::officialAccount($config[$msgType]);
  365. $is_office = $app->user->get($uid);
  366. $data = [
  367. 'touser' =>$uid,
  368. 'template_id' => 'IABFkUm4hoY_J44a767nADSSsGnsexUS4L-lEWJx2AA',
  369. // 'miniprogram' => [
  370. // 'appid' => env('WECHAT_APPID'),
  371. // 'pagepath' => '/pages/common_tools/my_consulting/consultingInfo?order_id='.$orderId,
  372. // ],
  373. 'url'=>'https://wechat.fresherbaby.com/admin',
  374. 'data' => [
  375. 'first' => '尊敬的客户,你有一笔'.$orderType.'订单已下单成功',
  376. 'keyword1' => $order->order_sn,
  377. 'keyword2' => ($order->total_amount/100).'元',
  378. 'keyword3' => '余额支付',
  379. 'remark' => '请耐心等待医生接单,如有疑问,请联系客服。',
  380. ]
  381. ];
  382. if($is_office){
  383. $app = Factory::officialAccount($config[3]);
  384. $data = [
  385. 'touser' =>'oYmUA5A1OIqtpA1XSrw35tbjtv1w',
  386. 'template_id' => 'M9b6PPKtD7PEqLsSnQ453iTIHtIZZpixOBjUjax8YXU',
  387. 'page' => '/pages/common_tools/my_consulting/consultingInfo?order_id='.$orderId,
  388. 'data' => [
  389. 'phrase1' => $orderType,
  390. 'name3' => $name,
  391. 'date4' => date('Y-m-d H:i:s'),
  392. 'thing8' => '你有一笔'.$orderType.'订单已成功下单请耐心等待',
  393. 'amount10' => ($order->total_amount/100).'元',
  394. ]
  395. ];
  396. }
  397. $res = $app->template_message->send($data);
  398. } else {
  399. $app = Factory::miniProgram($config[$msgType]);
  400. $res = $app->subscribe_message->send([
  401. 'touser' =>'oYmUA5A1OIqtpA1XSrw35tbjtv1w',
  402. 'template_id' => 'M9b6PPKtD7PEqLsSnQ453iTIHtIZZpixOBjUjax8YXU',
  403. 'page' => '/pages/common_tools/my_consulting/consultingInfo?order_id='.$orderId,
  404. 'data' => [
  405. 'phrase1' => $orderType,
  406. 'name3' => $name,
  407. 'date4' => date('Y-m-d H:i:s'),
  408. 'thing8' => '你有一笔'.$orderType.'订单已成功下单,请耐心等待',
  409. 'amount10' => ($order->total_amount/100).'元',
  410. ]
  411. ]);
  412. }
  413. if($res['errcode'] == 0) return out('',200,'发送成功') ;
  414. return out('',$res['errcode'],$res['errmsg']);
  415. }
  416. public function icons()
  417. {
  418. $type = request('type',1); //位置 1.首页 2.文章中心 3.个人中心
  419. $pType = request('ptype',1);//平台类型 1.用户端 2.医生端
  420. $data = Icons::where(['type'=>$type,'status'=>1,'type_cl'=>$pType])->orderBy('laval','desc')
  421. ->get(['id','name','image','laval'])->toArray();
  422. foreach ($data as $key=>$icons){
  423. $data[$key]['image'] = env('APP_URL').'upload/'.$icons['image'];
  424. }
  425. return out($data,200,'OK');
  426. }
  427. }