UserService.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\PreOrder;
  4. use GuzzleHttp\Client;
  5. use think\Db;
  6. use think\Exception;
  7. use think\Log;
  8. class UserService
  9. {
  10. private $model = null;
  11. /**
  12. * 初始化方法
  13. */
  14. public function __construct()
  15. {
  16. $this->model = Db::name('user');
  17. }
  18. /**
  19. * 用户换绑
  20. * @return void
  21. */
  22. public function userBindCompany($params=[])
  23. {
  24. $result = [
  25. 'status' => 1,
  26. 'msg' => '操作成功',
  27. 'data' => [],
  28. ];
  29. try {
  30. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  31. $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
  32. $where['id'] = $userId;
  33. $user = Db::name('user')->where($where['id'])->find();
  34. if (!empty($user)) {
  35. if ($user['company_id'] != $companyId) {
  36. $userData['company_id'] = $companyId;
  37. $userData['updatetime'] = time();
  38. $userRes = Db::name('user')->where($where['id'])->update($userData);
  39. if (!$userRes) {
  40. throw new Exception('操作失败');
  41. }
  42. }
  43. }
  44. } catch (Exception $e) {
  45. $result['status'] = 0;
  46. $result['msg'] = $e->getMessage();
  47. }
  48. return $result;
  49. }
  50. /**
  51. * 用户绑定门店和钱包
  52. * @return void
  53. */
  54. public function userWallet($params=[])
  55. {
  56. $result = [
  57. 'status' => 1,
  58. 'msg' => '操作成功',
  59. 'data' => [],
  60. ];
  61. try {
  62. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  63. $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
  64. $comefrom = isset($params['comefrom']) ? $params['comefrom'] : '';
  65. $bindRes = $this->userBindCompany($params);//绑定门店
  66. if (!$bindRes['status']) {
  67. throw new Exception($bindRes['msg']);
  68. }
  69. $where['user_id'] = $userId;
  70. $where['company_id'] = $companyId;
  71. $userWallet = Db::name('user_wallet')->where($where['id'])->find();
  72. $time = time();
  73. if (empty($userWallet)) {
  74. $userWalletData = [
  75. 'user_id' => $userId,
  76. 'company_id' => $companyId,
  77. 'money' => 0.00,
  78. 'createtime' => $time,
  79. 'comefrom' => $comefrom,
  80. ];
  81. $userWalletRes = Db::name('user_wallet')->insertGetId($userWalletData);
  82. if (!$userWalletRes) {
  83. throw new Exception('生成钱包失败');
  84. }
  85. }
  86. } catch (Exception $e) {
  87. $result['status'] = 0;
  88. $result['msg'] = $e->getMessage();
  89. }
  90. return $result;
  91. }
  92. /**
  93. * 预约消息
  94. * @return void
  95. */
  96. public function msgPreOrder($params=[])
  97. {
  98. $result = [
  99. 'status' => 1,
  100. 'msg' => '操作成功',
  101. 'data' => [],
  102. ];
  103. try {
  104. $preOrderId = isset($params['pre_order_id']) ? $params['pre_order_id'] : 0;
  105. $p = 'pre_order';
  106. $u = 'user';
  107. $c = 'company';
  108. $s = 'servicetype';
  109. $field = $p.'.*,'.$u.'.mini_openid,'.$c.'.name as `company_name`,'.$s.'.title as `service_title`';
  110. $where[$p.'.id'] = $preOrderId;
  111. $preOrder = Db::name($p)->alias($p)->field($field)
  112. ->join($u,$u.'.id = '.$p.'.user_id','LEFT')
  113. ->join($c,$c.'.id = '.$p.'.company_id','LEFT')
  114. ->join($s,$s.'.id = '.$p.'.servicetype_id','LEFT')
  115. ->where($where)->find();
  116. if (empty($preOrder)) {
  117. throw new Exception('未找到预约信息');
  118. }
  119. $statusArr = model('PreOrder')->getPreOrderStatusList();
  120. $statusText = isset($statusArr[$preOrder['pre_order_status']]) ? $statusArr[$preOrder['pre_order_status']] : '';
  121. $wechatTemplate = config('param.wechat_template');
  122. $preOrderMsg = isset($wechatTemplate['pre_order']) ? $wechatTemplate['pre_order'] : [];
  123. /*预约日期 {{time1.DATA}}
  124. 场地{{thing8.DATA}}
  125. 预约类型{{thing7.DATA}}
  126. 顾客称号{{thing3.DATA}}*/
  127. $data = [//数据内容
  128. "time1" => ["value" => date('Y年m月d日 H:i',$preOrder['pre_time'])],
  129. "thing8" => ["value" => $preOrder['company_name'] .'的预约'.$statusText],
  130. "thing7" => ["value" => $preOrder['service_title']],
  131. "thing3" => ["value" => $preOrder['name']],
  132. ];
  133. $paramsData = $preOrderMsg;
  134. $paramsData['mini_openid'] = $preOrder['mini_openid'];
  135. $paramsData['data'] = $data;
  136. $msgRes = $this->wechatMessageSend($paramsData);
  137. if (!$msgRes['status']) {
  138. throw new Exception($msgRes['msg']);
  139. }
  140. } catch (Exception $e) {
  141. $result['status'] = 0;
  142. $result['msg'] = $e->getMessage();
  143. $errorData = [
  144. 'pre_order_id' => $preOrderId,
  145. 'params' => isset($paramsData) ? $paramsData : [],
  146. 'error' => $result['msg'],
  147. ];
  148. Log::error(json_encode($errorData));
  149. }
  150. return $result;
  151. }
  152. /**
  153. * 订单消息
  154. * @return void
  155. */
  156. public function msgOrder($params=[])
  157. {
  158. $result = [
  159. 'status' => 1,
  160. 'msg' => '操作成功',
  161. 'data' => [],
  162. ];
  163. try {
  164. $orderId = isset($params['order_id']) ? $params['order_id'] : 0;
  165. $o = 'order';
  166. $u = 'user';
  167. $where[$o.'.id'] = $orderId;
  168. $field = $o.'.*,'.$u.'.mini_openid';
  169. $order = Db::name('order')->alias($o)->field($field)
  170. ->join($u,$u.'.id = '.$o.'.user_id','LEFT')
  171. ->where($where)->find();
  172. if (empty($order)) {
  173. throw new Exception('未找到订单信息');
  174. }
  175. $statusArr = model('Order')->getStatusList();
  176. $statusText = isset($statusArr[$order['status']]) ? $statusArr[$order['status']] : '';
  177. $wechatTemplate = config('param.wechat_template');
  178. $orderMsg = isset($wechatTemplate['order']) ? $wechatTemplate['order'] : [];
  179. /*工单号{{character_string1.DATA}}
  180. 处理进度 {{phrase4.DATA}}
  181. 提交时间 {{time2.DATA}}*/
  182. $data = [//数据内容
  183. "character_string1" => ["value" => $order['orderno']],
  184. "phrase4" => ["value" => $statusText],
  185. "time2" => ["value" => date('Y年m月d日 H:i:s',$order['createtime'])],
  186. ];
  187. $orderMsg['page'] = $orderMsg['page'].'?id='.$order['id'];
  188. $paramsData = $orderMsg;
  189. $paramsData['mini_openid'] = $order['mini_openid'];
  190. $paramsData['data'] = $data;
  191. $msgRes = $this->wechatMessageSend($paramsData);
  192. if (!$msgRes['status']) {
  193. throw new Exception($msgRes['msg']);
  194. }
  195. } catch (Exception $e) {
  196. $result['status'] = 0;
  197. $result['msg'] = $e->getMessage();
  198. $errorData = [
  199. 'order_id' => $orderId,
  200. 'params' => isset($paramsData) ? $paramsData : [],
  201. 'error' => $result['msg'],
  202. ];
  203. Log::error(json_encode($errorData));
  204. }
  205. return $result;
  206. }
  207. /**
  208. * 微信消息发送
  209. * @return void
  210. */
  211. public function wechatMessageSend($params=[])
  212. {
  213. $result = [
  214. 'status' => 1,
  215. 'msg' => '操作成功',
  216. 'data' => [],
  217. ];
  218. try {
  219. $client = new Client();
  220. $tk = getAccessToken();
  221. $uri = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$tk;
  222. $jsonData = [
  223. "touser" => isset($params['mini_openid']) ? $params['mini_openid'] : '', //openid
  224. "template_id" => isset($params['template_id']) ? $params['template_id'] : '', //模版ID
  225. "page" => isset($params['page']) ? $params['page'] : '', //跳转地址
  226. "miniprogram_state" => isset($params['miniprogram_state']) ? $params['miniprogram_state'] : '',//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
  227. "lang" => isset($params['lang']) ? $params['lang'] : '', //支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
  228. "data" => isset($params['data']) ? $params['data'] : [], //数据
  229. ];
  230. $res = $client->request('POST', $uri, ['json' => $jsonData]);
  231. $returnResJson = $res->getBody()->getContents();
  232. $returnRes = json_decode($returnResJson, true);
  233. if ($returnRes['errcode'] != 0) {
  234. throw new Exception($returnRes['errmsg']);
  235. }
  236. } catch (Exception $e) {
  237. $result['status'] = 0;
  238. $result['msg'] = $e->getMessage();
  239. }
  240. return $result;
  241. }
  242. }