UserService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. $wechatSetting = config('param.wechat_setting');
  124. if ($wechatSetting == 'release') {//正式
  125. /*预约时间:{{time2.DATA}}
  126. 网点名称: {{thing3.DATA}}
  127. 类型: {{thing1.DATA}}*/
  128. $data = [//数据内容
  129. "time2" => ["value" => date('Y年m月d日 H:i',$preOrder['pre_time'])],
  130. "thing3" => ["value" => $preOrder['company_name'] .'的预约'.$statusText],
  131. "thing1" => ["value" => $preOrder['service_title']],
  132. ];
  133. } else {
  134. /*预约日期 {{time1.DATA}}
  135. 场地{{thing8.DATA}}
  136. 预约类型{{thing7.DATA}}
  137. 顾客称号{{thing3.DATA}}*/
  138. $data = [//数据内容
  139. "time1" => ["value" => date('Y年m月d日 H:i',$preOrder['pre_time'])],
  140. "thing8" => ["value" => $preOrder['company_name'] .'的预约'.$statusText],
  141. "thing7" => ["value" => $preOrder['service_title']],
  142. "thing3" => ["value" => $preOrder['name']],
  143. ];
  144. }
  145. $paramsData = $preOrderMsg;
  146. $paramsData['mini_openid'] = $preOrder['mini_openid'];
  147. $paramsData['data'] = $data;
  148. $msgRes = $this->wechatMessageSend($paramsData);
  149. if (!$msgRes['status']) {
  150. throw new Exception($msgRes['msg']);
  151. }
  152. } catch (Exception $e) {
  153. $result['status'] = 0;
  154. $result['msg'] = $e->getMessage();
  155. $errorData = [
  156. 'pre_order_id' => $preOrderId,
  157. 'params' => isset($paramsData) ? $paramsData : [],
  158. 'error' => $result['msg'],
  159. ];
  160. Log::error(json_encode($errorData));
  161. }
  162. return $result;
  163. }
  164. /**
  165. * 订单消息
  166. * @return void
  167. */
  168. public function msgOrder($params=[])
  169. {
  170. $result = [
  171. 'status' => 1,
  172. 'msg' => '操作成功',
  173. 'data' => [],
  174. ];
  175. try {
  176. $orderId = isset($params['order_id']) ? $params['order_id'] : 0;
  177. $o = 'order';
  178. $u = 'user';
  179. $where[$o.'.id'] = $orderId;
  180. $field = $o.'.*,'.$u.'.mini_openid';
  181. $order = Db::name('order')->alias($o)->field($field)
  182. ->join($u,$u.'.id = '.$o.'.user_id','LEFT')
  183. ->where($where)->find();
  184. if (empty($order)) {
  185. throw new Exception('未找到订单信息');
  186. }
  187. $statusArr = model('Order')->getStatusList();
  188. $statusText = isset($statusArr[$order['status']]) ? $statusArr[$order['status']] : '';
  189. $wechatTemplate = config('param.wechat_template');
  190. $orderMsg = isset($wechatTemplate['order']) ? $wechatTemplate['order'] : [];
  191. $wechatSetting = config('param.wechat_setting');
  192. if ($wechatSetting == 'release') {//正式
  193. /*工单号: {{character_string5.DATA}}
  194. 状态: {{phrase4.DATA}}
  195. 下单时间:{{time8.DATA}}*/
  196. $data = [//数据内容
  197. "character_string5" => ["value" => $order['orderno']],
  198. "phrase4" => ["value" => $statusText],
  199. "time8" => ["value" => date('Y年m月d日 H:i:s',$order['createtime'])],
  200. ];
  201. } else {
  202. /*工单号{{character_string1.DATA}}
  203. 处理进度 {{phrase4.DATA}}
  204. 提交时间 {{time2.DATA}}*/
  205. $data = [//数据内容
  206. "character_string1" => ["value" => $order['orderno']],
  207. "phrase4" => ["value" => $statusText],
  208. "time2" => ["value" => date('Y年m月d日 H:i:s',$order['createtime'])],
  209. ];
  210. }
  211. $orderMsg['page'] = $orderMsg['page'].'?id='.$order['id'];
  212. $paramsData = $orderMsg;
  213. $paramsData['mini_openid'] = $order['mini_openid'];
  214. $paramsData['data'] = $data;
  215. $msgRes = $this->wechatMessageSend($paramsData);
  216. if (!$msgRes['status']) {
  217. throw new Exception($msgRes['msg']);
  218. }
  219. } catch (Exception $e) {
  220. $result['status'] = 0;
  221. $result['msg'] = $e->getMessage();
  222. $errorData = [
  223. 'order_id' => $orderId,
  224. 'params' => isset($paramsData) ? $paramsData : [],
  225. 'error' => $result['msg'],
  226. ];
  227. Log::error(json_encode($errorData));
  228. }
  229. return $result;
  230. }
  231. /**
  232. * 微信消息发送
  233. * @return void
  234. */
  235. public function wechatMessageSend($params=[])
  236. {
  237. $result = [
  238. 'status' => 1,
  239. 'msg' => '操作成功',
  240. 'data' => [],
  241. ];
  242. try {
  243. $client = new Client();
  244. $tk = getAccessToken();
  245. $uri = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$tk;
  246. $jsonData = [
  247. "touser" => isset($params['mini_openid']) ? $params['mini_openid'] : '',//openid
  248. "template_id" => isset($params['template_id']) ? $params['template_id'] : '',//模版ID
  249. "page" => isset($params['page']) ? $params['page'] : '', //跳转地址
  250. "miniprogram_state" => isset($params['miniprogram_state']) ? $params['miniprogram_state'] : '',//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
  251. "lang" => isset($params['lang']) ? $params['lang'] : 'zh_CN', //支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
  252. "data" => isset($params['data']) ? $params['data'] : [], //数据
  253. ];
  254. $res = $client->request('POST', $uri, ['json' => $jsonData]);
  255. $returnResJson = $res->getBody()->getContents();
  256. $returnRes = json_decode($returnResJson, true);
  257. if ($returnRes['errcode'] != 0) {
  258. throw new Exception($returnRes['errmsg']);
  259. }
  260. } catch (Exception $e) {
  261. $result['status'] = 0;
  262. $result['msg'] = $e->getMessage();
  263. }
  264. return $result;
  265. }
  266. }