CustomController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. <?php
  2. namespace shop\controllers;
  3. use biz\admin\services\AdminService;
  4. use biz\custom\classes\CustomClass;
  5. use biz\merchant\services\MerchantAssetService;
  6. use biz\merchant\services\MerchantRemarkService;
  7. use biz\user\services\UserAssetService;
  8. use biz\user\services\UserGrowthService;
  9. use biz\user\services\UserService;
  10. use biz\user\services\UserUpgradeService;
  11. use common\components\wxUtil;
  12. use common\services\xhMerchantExtendService;
  13. use common\services\xhRechargeService;
  14. use common\services\xhTMessageService;
  15. use common\services\xhUserIntegralService;
  16. use Yii;
  17. use common\components\stringUtil;
  18. use common\components\util;
  19. use biz\merchant\services\MerchantExtendService;
  20. use biz\message\services\SmsService;
  21. use biz\user\classes\UserClass;
  22. use yii\helpers\Json;
  23. class CustomController extends BaseController
  24. {
  25. //客户列表 shish 2019.11.30
  26. public function actionList()
  27. {
  28. $get = Yii::$app->request->get();
  29. $type = isset($get['type']) ? $get['type'] : -1;
  30. $search = isset($get['search']) ? $get['search'] : '';
  31. $where = ['merchantId' => $this->sjId];
  32. switch ($type) {
  33. case 1:
  34. //粉丝
  35. $where['subscribe'] = 1;
  36. break;
  37. case 2:
  38. //会员
  39. $where['member>'] = 0;
  40. break;
  41. default:
  42. }
  43. if (!empty($search)) {
  44. if (stringUtil::isMobile($search)) {
  45. $where['mobile'] = $search;
  46. } else {
  47. $where['userName'] = ['like', $search];
  48. }
  49. }
  50. $list = UserService::getUserList($where);
  51. $list['asset'] = MerchantAssetService::getByMerchantId($this->sjId);
  52. util::success($list);
  53. }
  54. //获取客户基本和资产信息 shish 2019.11.30
  55. public function actionDetail()
  56. {
  57. $userId = Yii::$app->request->get('userId');
  58. $info = UserService::getUserInfo($userId);
  59. if (isset($info['merchantId']) == false || $this->sjId != $info['merchantId']) {
  60. util::fail('没有权限');
  61. }
  62. util::success($info);
  63. }
  64. //获取客户详情,包括基本信息、资产、订单和备注 2019.11.30
  65. public function actionUserDetail()
  66. {
  67. $userId = Yii::$app->request->get('userId');
  68. $info = UserService::getFullUserInfo($userId);
  69. UserService::valid($info, $this->sjId);
  70. $list = MerchantRemarkService::getRemarkList($userId);
  71. $info['remarkList'] = $list;
  72. util::success($info);
  73. }
  74. //添加客户,申请会员时会自动同步资产 shish 2019.11.30
  75. public function actionAdd()
  76. {
  77. $post = Yii::$app->request->post();
  78. if (isset($post['mobile']) == false || stringUtil::isMobile($post['mobile']) == false) {
  79. util::fail('请输入手机号');
  80. }
  81. if ($post['mobile'] != $post['confirmMobile']) {
  82. util::fail('二次输入手机号不一样');
  83. }
  84. $confirmPassword = isset($post['confirmPassword']) ? $post['confirmPassword'] : '';
  85. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  86. $mobile = $post['mobile'];
  87. $user = UserService::getByMobile($mobile, $this->sjId);
  88. if (!empty($user)) {
  89. util::fail('手机号已使用');
  90. }
  91. $post['merchantId'] = $this->sjId;
  92. $post['member'] = $post['memberLevel'];
  93. UserService::addUser($post);
  94. util::complete();
  95. }
  96. //充值 shish 2019.12.1
  97. public function actionRecharge()
  98. {
  99. $post = Yii::$app->request->post();
  100. $confirmPassword = isset($post['confirmPassword']) ? $post['confirmPassword'] : '';
  101. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  102. $userId = isset($post['userId']) ? $post['userId'] : 0;
  103. $rechargeAmount = isset($post['rechargeAmount']) ? $post['rechargeAmount'] : 1;
  104. $user = UserService::getById($userId);
  105. if (empty($user)) {
  106. util::fail('找不到客户');
  107. }
  108. if ($user['merchantId'] != $this->sjId) {
  109. util::fail('您没有权限操作');
  110. }
  111. $userAsset = UserAssetService::getByUserId($userId);
  112. $totalBalance = $userAsset['balance'] + $rechargeAmount;
  113. $totalRecharge = $userAsset['totalRecharge'] + $rechargeAmount;
  114. $upData['balance'] = $totalBalance;
  115. $upData['totalRecharge'] = $totalRecharge;
  116. $merchantAsset = MerchantAssetService::getByMerchantId($this->sjId);
  117. $return = xhRechargeService::recharge($rechargeAmount, $user, $this->adminId, $this->sj, $this->sjExtend, $merchantAsset);
  118. if ($return['code'] == 'A0001') {
  119. util::complete();
  120. }
  121. util::fail();
  122. }
  123. //升级 shish 2019.12.1
  124. public function actionUpgrade()
  125. {
  126. $post = Yii::$app->request->post();
  127. $userId = isset($post['userId']) ? $post['userId'] : 0;
  128. $member = isset($post['member']) ? $post['member'] : 0;
  129. $user = UserService::getById($userId);
  130. if (empty($user)) {
  131. util::fail('客户不存在');
  132. }
  133. //验证是否商家客户
  134. UserService::valid($user, $this->sjId);
  135. //验证确认密码
  136. $confirmPassword = $post['confirmPassword'];
  137. AdminService::verifyConfirmPassword($this->adminId, $confirmPassword);
  138. $grade = xhMerchantExtendService::getGradeList($this->sjExtend, 'desc');
  139. //升级后的成长值
  140. $newLevelGrowth = $grade[$member]['growth'];
  141. $newDiscount = $grade[$member]['discount'];
  142. $userAsset = UserAssetService::getByUserId($userId);
  143. if ($userAsset['member'] >= $member) {
  144. util::fail('客户已经达到此级别了');
  145. }
  146. $now = time();
  147. $date = date("Y-m-d H:i", $now);
  148. $connection = Yii::$app->db;//事务处理
  149. $transaction = $connection->beginTransaction();
  150. try {
  151. $addGrowth = $newLevelGrowth - $userAsset['growth'];
  152. $upData['growth'] = $newLevelGrowth;
  153. $upData['member'] = $member;
  154. $upData['discount'] = $newDiscount;
  155. UserAssetService::updateByUserId($userId, $upData);
  156. UserService::updateById($userId, ['member' => $member]);
  157. $upgradeData = [
  158. 'member' => $member,
  159. 'prevMember' => $userAsset['member'],
  160. 'merchantId' => $this->sjId,
  161. 'gainType' => 0,
  162. 'addTime' => $now,
  163. 'userId' => $userId,
  164. ];
  165. $upgradeRespond = UserUpgradeService::add($upgradeData);
  166. $growthData = [
  167. 'userId' => $userId,
  168. 'userName' => $user['userName'],
  169. 'merchantId' => $this->sjId,
  170. 'relateId' => $upgradeRespond['id'],
  171. 'growth' => $newLevelGrowth,
  172. 'num' => $addGrowth,
  173. 'io' => 1,
  174. 'payWay' => 4,
  175. 'alipayId' => '',
  176. 'event' => '管理员操作升到' . $member . '级增加成长值',
  177. 'operatorId' => $userId,
  178. 'createTime' => $date,
  179. 'gainType' => 0,
  180. 'addTime' => $now,
  181. ];
  182. UserGrowthService::add($growthData);
  183. $transaction->commit();
  184. } catch (Exception $e) {
  185. $transaction->rollBack();
  186. util::fail();
  187. }
  188. $allTM = xhTMessageService::getList($this->sjId);
  189. $openId = isset($user['openId']) ? $user['openId'] : '';
  190. if (empty($openId)) {
  191. util::complete();
  192. }
  193. $shortTempId = 'OPENTM205211943';//升级通知
  194. if (isset($allTM[$shortTempId]) == false) {
  195. util::complete();
  196. }
  197. $tempId = $allTM[$shortTempId];
  198. $data = [
  199. "touser" => $openId, "template_id" => $tempId, "url" => '',
  200. "data" => [
  201. "first" => ["value" => "恭喜,您已经升为{$member}级会员。", "color" => "#173177"],
  202. "keyword1" => ["value" => $user['userName'], "color" => "#173177"],
  203. "keyword2" => ["value" => $date, "color" => "#173177"],
  204. "remark" => ["value" => "点击查看会员权益", "color" => "#173177"],
  205. ]];
  206. wxUtil::sendTaskInform($data, $this->sj);
  207. util::complete();
  208. }
  209. //重置客户密码
  210. public function actionResetPayPassword()
  211. {
  212. $post = Yii::$app->request->post();
  213. $userId = isset($post['userId']) ? $post['userId'] : 0;
  214. $password = isset($post['password']) ? $post['password'] : 0;
  215. $info = UserService::getById($userId);
  216. UserService::valid($info, $this->sjId);
  217. UserService::updateById($userId, ['payPassword' => password_hash($password, PASSWORD_BCRYPT)]);
  218. util::complete();
  219. }
  220. //获取登陆客户的资产 shish 2019.11.22
  221. public function actionAsset()
  222. {
  223. $asset = UserAssetService::getByUserId($this->adminId);
  224. util::success($asset);
  225. }
  226. //获取登陆客户的优惠情况 shish 2019.12.3
  227. public function actionDiscount()
  228. {
  229. $discount = UserAssetService::getDiscount($this->adminId);
  230. util::success($discount);
  231. }
  232. //小程序用户完整信息 shish 2019.12.12
  233. public function actionMiniFullInfo()
  234. {
  235. $post = Yii::$app->request->post();
  236. $iv = isset($post['iv']) ? $post['iv'] : '';
  237. $encryptedData = isset($post['encryptedData']) ? $post['encryptedData'] : '';
  238. $appId = $this->sj['miniAppId'];
  239. $merchantId = $this->sjId;
  240. $miniOpenId = $this->user['miniOpenId'];
  241. if (empty($miniOpenId)) {
  242. util::fail('mini_open_id empty');
  243. }
  244. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  245. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  246. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  247. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  248. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  249. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  250. if ($errCode != 0) {
  251. Yii::info($result . ' ' . $errCode);
  252. util::fail('获取用户信息失败');
  253. }
  254. Yii::info('小程序获取用户信息:' . $result);
  255. $originalInfo = Json::decode($result);
  256. $source = UserClass::$userSourceId['mini']['name'];
  257. $user = UserService::replaceUser($originalInfo, $source, $merchantId);
  258. $userId = $user['id'];
  259. $userInfo = UserService::getUserInfo($userId);
  260. util::success($userInfo);
  261. }
  262. //小程序用户手机号
  263. public function actionMiniMobile()
  264. {
  265. $post = Yii::$app->request->post();
  266. $iv = $post['iv'];
  267. $encryptedData = $post['encryptedData'];
  268. $merchantId = $this->sjId;
  269. $merchant = $this->sj;
  270. $appId = $merchant['miniAppId'];
  271. $miniOpenId = $this->user['miniOpenId'];
  272. $user = $this->user;
  273. if (!empty($user) && !empty($user['mobile'])) {
  274. $userId = $user['id'];
  275. util::success(['mobile' => $user['mobile']]);
  276. }
  277. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  278. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  279. if (empty($sessionKey)) {
  280. util::fail('系统错误,sesstion empty');
  281. }
  282. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  283. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  284. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  285. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  286. if ($errCode != 0) {
  287. util::fail('解密失败');
  288. }
  289. $arr = Json::decode($result);
  290. $mobile = isset($arr['purePhoneNumber']) ? $arr['purePhoneNumber'] : '';
  291. if (empty($mobile)) {
  292. util::fail('没有获取手机号,请重试');
  293. }
  294. $userId = $user['id'];
  295. UserService::becomeMember($user, $merchant, ['mobile' => $mobile]);
  296. util::success(['mobile' => $mobile]);
  297. }
  298. //密码修改
  299. public function actionPassword()
  300. {
  301. $post = Yii::$app->request->post();
  302. $old = isset($post['old']) ? $post['old'] : '';
  303. $new = isset($post['new']) ? $post['new'] : '';
  304. $confirm = isset($post['confirm']) ? $post['confirm'] : '';
  305. if (empty($new)) {
  306. util::fail('请输入新密码');
  307. }
  308. if ($new != $confirm) {
  309. util::fail('二次密码不一致');
  310. }
  311. $userId = $this->adminId;
  312. $user = UserService::getUserInfo($userId, false);
  313. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  314. if (empty($old)) {
  315. util::fail('请填写旧密码');
  316. }
  317. if (password_verify($old, $user['payPassword']) == false) {
  318. util::fail('旧密码错误');
  319. }
  320. }
  321. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  322. util::complete('修改成功');
  323. }
  324. //申请会员 shish 2019.12.18
  325. public function actionApplyMember()
  326. {
  327. $post = Yii::$app->request->post();
  328. $mobile = isset($post['mobile']) ? $post['mobile'] : 0;
  329. if (stringUtil::isMobile($mobile) == false) {
  330. util::fail('请输入手机号');
  331. }
  332. $userId = $this->adminId;
  333. $asset = UserAssetService::getByUserId($userId);
  334. if (isset($asset['member']) && $asset['member'] != -1) {
  335. util::fail('已经是会员了');
  336. }
  337. $code = isset($post['code']) ? $post['code'] : '';
  338. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  339. if (empty($cacheCode) || $cacheCode != $code) {
  340. util::fail('验证码错误');
  341. }
  342. $update = [];
  343. if (isset($post['realName']) && !empty($post['realName'])) {
  344. $update['realName'] = $post['realName'];
  345. }
  346. if (isset($post['birthday']) && !empty($post['birthday'])) {
  347. $update['birthday'] = $post['birthday'];
  348. }
  349. $user = UserService::getByCondition(['merchantId' => $this->sjId, 'mobile' => $mobile]);
  350. if ($userId == $user['id']) {
  351. util::fail('手机号已验证');
  352. }
  353. if (!empty($user)) {
  354. util::fail('手机号已经注册过了');
  355. }
  356. $update['mobile'] = $mobile;
  357. UserService::becomeMember($this->user, $this->sj, $update);
  358. util::complete('注册成功');
  359. }
  360. //用户基本信息和资产 shish 2019.12.19
  361. public function actionMyProfile()
  362. {
  363. $custom = $this->custom;
  364. $user = UserService::getUserInfo($this->adminId);
  365. $growth = $custom['growth'] ?? 0;
  366. $extend = $this->sjExtend;
  367. $grade = MerchantExtendService::getGradeList($extend, 'asc');
  368. //下个等级的积分
  369. $nextLevelGrowth = 0;
  370. foreach ($grade as $val) {
  371. $currentGrowth = $val['growth'];
  372. if ($currentGrowth > $growth) {
  373. $nextLevelGrowth = $currentGrowth;
  374. break;
  375. }
  376. }
  377. $user['userAsset']['nextLevelGrowth'] = $nextLevelGrowth;
  378. util::success($user);
  379. }
  380. //登陆客户的信息 shish 2020.3.12
  381. public function actionLoginDetail()
  382. {
  383. $user = $this->user;
  384. util::success($user);
  385. }
  386. }