UserService.php 11 KB

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