UserService.php 11 KB

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