UserController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\controllers;
  8. use app\models\Admin;
  9. use app\models\AgentApply;
  10. use app\models\BrowseLog;
  11. use app\models\District;
  12. use app\models\Favorite;
  13. use app\models\Level;
  14. use app\models\MdStaff;
  15. use app\models\SaaSLeaguePriceLog;
  16. use app\models\SaasUser;
  17. use app\models\ShareHolder;
  18. use app\models\Store;
  19. use app\models\User;
  20. use app\models\UserCard;
  21. use app\models\UserCoupon;
  22. use app\models\VerifyCardSale;
  23. use app\models\VideoGoodsAuthor;
  24. use app\modules\alliance\models\UserCenterForm;
  25. use app\modules\alliance\controllers\BaseController;
  26. use app\modules\alliance\models\OrderListForm;
  27. use app\modules\alliance\models\ShareForm;
  28. use app\modules\alliance\models\FavoriteListForm;
  29. use app\utils\ShareQrcode;
  30. use EasyWeChat\Kernel\Exceptions\HttpException;
  31. /**
  32. * 用户
  33. */
  34. class UserController extends BaseController
  35. {
  36. public function behaviors()
  37. {
  38. return parent::behaviors();
  39. }
  40. /**
  41. * 获取店铺联盟券
  42. *
  43. */
  44. public function actionGetStoreLeaguePrice()
  45. {
  46. $form = new FavoriteListForm();
  47. $access_token = get_params('access_token');
  48. $saas_user_model = SaasUser::findOne(['access_token' => $access_token]);
  49. $saas_user_id = $saas_user_model->id;
  50. return $this->asJson($form->league_search($saas_user_id));
  51. }
  52. public function actionGetUserName()
  53. {
  54. $mobile = get_params('mobile');
  55. $saas_user = SaasUser::findOne(['mobile' => $mobile]);
  56. if($saas_user){
  57. return $this->asJson([
  58. 'code' => 0,
  59. 'data' => $saas_user->name,
  60. 'msg' => '查找成功'
  61. ]);
  62. }else{
  63. return $this->asJson([
  64. 'code' => 1,
  65. 'data' => null,
  66. 'msg' => '未找到'
  67. ]);
  68. }
  69. }
  70. /**
  71. * 转送联盟券
  72. */
  73. public function actionPostSwitchLeaguePrice()
  74. {
  75. $access_token = post_params('access_token');
  76. $from_saas_user_model = SaasUser::findOne(['access_token' => $access_token]);
  77. $from_saas_user_id = $from_saas_user_model->id;
  78. $admin = Admin::findOne(['saas_user_id' => $from_saas_user_id, 'is_delete' => 0, 'type' => 'store']);
  79. $store = Store::findOne($admin->type_id);
  80. $league_price = post_params('league_price');
  81. $to_saas_user_mobile = post_params('mobile');
  82. $saas_user_model = SaasUser::findOne(['mobile' => $to_saas_user_mobile]);
  83. if($league_price <= 0){
  84. return $this->asJson([
  85. 'code' => 1,
  86. 'msg' => '提交联盟券金额不正确'
  87. ]);
  88. }
  89. if($saas_user_model){
  90. if($store->league_price < $league_price){
  91. return $this->asJson([
  92. 'code' => 1,
  93. 'data' => $store->id,
  94. 'msg' => '当前店铺联盟券金额不足'
  95. ]);
  96. }else{
  97. //给商家扣减 (用户赠送的联盟券钱从商家从扣减)
  98. $before = $store->league_price;
  99. $store->updateCounters(['league_price' => -floatval($league_price)]);
  100. \app\models\SaaSLeaguePriceLog::setLeaguePriceLog(
  101. $store->id,
  102. $saas_user_model->id,
  103. $league_price,
  104. $before,
  105. \app\models\SaaSLeaguePriceLog::TYPE_SEND,
  106. \app\models\SaaSLeaguePriceLog::TAKE_TYPE,
  107. \app\models\SaaSLeaguePriceLog::ROLE_STORE
  108. );
  109. $before = $saas_user_model->league_price;
  110. $saas_user_model->updateCounters(['league_price' => floatval($league_price)]);
  111. $id = 0;
  112. \app\models\SaaSLeaguePriceLog::setLeaguePriceLog(
  113. $store->id,
  114. $saas_user_model->id,
  115. $league_price,
  116. $before,
  117. \app\models\SaaSLeaguePriceLog::TYPE_SEND,
  118. \app\models\SaaSLeaguePriceLog::SEND_TYPE,
  119. \app\models\SaaSLeaguePriceLog::ROLE_USER,
  120. 0,
  121. $id
  122. );
  123. return $this->asJson([
  124. 'code' => 0,
  125. 'data' => $id,
  126. 'msg' => '转赠成功!'
  127. ]);
  128. }
  129. }else{
  130. return $this->asJson([
  131. 'code' => 1,
  132. 'msg' => '未找到用户'
  133. ]);
  134. }
  135. }
  136. /**
  137. * 喜欢的商品或店铺列表
  138. */
  139. public function actionFavoriteList()
  140. {
  141. $form = new FavoriteListForm();
  142. $form->attributes = get_params();
  143. $form->user_id = get_user_id();
  144. $form->store_id = get_store_id();
  145. return $this->asJson($form->search());
  146. }
  147. /**
  148. * 修改用户信息
  149. */
  150. public function actionEditUserInfo()
  151. {
  152. $saasUser = get_saas_user();
  153. if ($saasUser) {
  154. $avatar_url = input_params('avatar_url', '');
  155. $nickname = input_params('nickname', '');
  156. $gender = input_params('gender', '');
  157. if (!empty($avatar_url)) {
  158. $saasUser->avatar = $avatar_url;
  159. }
  160. if (!empty($nickname)) {
  161. $saasUser->name = $nickname;
  162. }
  163. if (!empty($gender) && in_array($gender, [0, 1, 2])) {
  164. $saasUser->gender = $gender;
  165. }
  166. if ($saasUser->save()) {
  167. return $this->asJson([
  168. 'code' => 0,
  169. 'msg' => '保存成功',
  170. ]);
  171. }
  172. return $this->asJson([
  173. 'code' => 1,
  174. 'msg' => '保存失败',
  175. ]);
  176. }
  177. return $this->asJson([
  178. 'code' => 1,
  179. 'msg' => '非法访问',
  180. ]);
  181. }
  182. /**
  183. * 积分记录
  184. */
  185. public function actionUserIntegralLog()
  186. {
  187. $form = new UserCenterForm();
  188. $result = $form->getIntegralLog();
  189. return $this->asJson($result);
  190. }
  191. /**
  192. * 更新用户wechat_app_open_id
  193. */
  194. public function actionUpdateWechatAppOpenId()
  195. {
  196. $id = input_params('user_id');
  197. $openid = input_params('open_id');
  198. if (!$id || !$openid) {
  199. return $this->asJson([
  200. 'code' => 1,
  201. 'msg' => '缺少参数',
  202. ]);
  203. }
  204. $user = User::findOne($id);
  205. if (!$user) {
  206. return $this->asJson([
  207. 'code' => 1,
  208. 'msg' => '用户未找到',
  209. ]);
  210. }
  211. $user->wechat_app_open_id = $openid;
  212. $user->save();
  213. return $this->asJson([
  214. 'code' => 0,
  215. 'msg' => '更新成功',
  216. ]);
  217. }
  218. public function actionGetAgentApply() {
  219. try {
  220. $saas_user = get_saas_user();
  221. $params = post_params();
  222. $apply_ = AgentApply::findOne(['saas_user_id' => $saas_user->id, 'agent_type' => $params['type'] ?: 0, 'is_delete' => 0]);
  223. $readme = [
  224. 'area_agent_apply_agreement' => \app\models\Option::get('area_agent_apply_agreement', 0, 'saas', '')['value'],
  225. 'bd_agent_apply_agreement' => \app\models\Option::get('bd_agent_apply_agreement', 0, 'saas', '')['value'],
  226. 'goods_agent_apply_agreement' => \app\models\Option::get('goods_agent_apply_agreement', 0, 'saas', '')['value'],
  227. ];
  228. if (!$apply_) {
  229. return $this->asJson(array_merge([
  230. 'code' => 0,
  231. 'msg' => '获取成功',
  232. 'data' => [
  233. 'province_id' => '',
  234. 'city_id' => '',
  235. 'district_id' => '',
  236. 'address' => '',
  237. 'lat' => '',
  238. 'lng' => '',
  239. 'area_level' => '',
  240. 'agent_type' => 0,
  241. 'status' => -1
  242. ]
  243. ], $readme));
  244. }
  245. $city_arr = District::find()->where(['id' => [$apply_->province_id, $apply_->city_id, $apply_->district_id]])
  246. ->select('name')->column();
  247. $city = implode('/', $city_arr);
  248. $data = [
  249. "province_id" => $apply_->province_id,
  250. "city_id" => $apply_->city_id,
  251. "district_id" => $apply_->district_id,
  252. "address" => $apply_->address,
  253. "lat" => $apply_->lat,
  254. "lng" => $apply_->lng,
  255. "area_level" => $apply_->area_level,
  256. "status" => (string)$apply_->status,
  257. 'agent_type' => $apply_->agent_type,
  258. 'city' => $city
  259. ];
  260. return $this->asJson(array_merge([
  261. 'code' => 0,
  262. 'msg' => 'success',
  263. 'data' => $data
  264. ], $readme));
  265. } catch (\Exception $e) {
  266. return $this->asJson([
  267. 'code' => 1,
  268. 'msg' => $e->getMessage(),
  269. ]);
  270. }
  271. }
  272. public function actionApplyAgent() {
  273. try {
  274. $saas_user = get_saas_user();
  275. $params = post_params();
  276. $username = $saas_user->mobile . "_area_agent";
  277. //区域代理
  278. $type = 'admin';
  279. //推广代理
  280. if ((int)$params['type'] === 1) {
  281. $username = $saas_user->mobile . "_bd_agent";
  282. $type = 'bd_agent';
  283. }
  284. //产品代理
  285. if ((int)$params['type'] === 2) {
  286. $username = $saas_user->mobile . "_g_agent";
  287. $type = 'goods_agent';
  288. }
  289. $apply_ = AgentApply::findOne(['saas_user_id' => $saas_user->id, 'agent_type' => $params['type'], 'is_delete' => 0]);
  290. $admin_ = Admin::findOne(['saas_user_id' => $saas_user->id, 'type' => $type, 'is_delete' => 0]);
  291. if ($admin_) {
  292. throw new \Exception('当前用户已经绑定代理');
  293. }
  294. if ($apply_ && in_array($apply_->status, [0, 1])) {
  295. throw new \Exception('当前用户已经绑定代理或审核中');
  296. }
  297. //判断是否存在同区域同等级代理
  298. if ((int)$params['type'] === 0) {
  299. $models = [
  300. [
  301. 'type' => 'admin',
  302. 'model' => Admin::find()
  303. ], [
  304. 'type' => 'apply',
  305. 'model' => AgentApply::find()
  306. ]
  307. ];
  308. foreach ($models as $item) {
  309. $admin_query = $item['model']->where(['province_id' => $params['province_id'], 'is_delete' => 0]);
  310. if ($item['type'] === 'admin') {
  311. $admin_query->andWhere(['type' => 'admin']);
  312. } else {
  313. $admin_query->andWhere(['agent_type' => 0, 'status' => [0]]);
  314. }
  315. if ((int)$params['area_level'] === 3) {
  316. $admin_query->andWhere(['area_level' => 3]);
  317. } elseif ((int)$params['area_level'] === 2) {
  318. $admin_query->andWhere(['city_id' => $params['city_id'], 'area_level' => 2]);
  319. } elseif ((int)$params['area_level'] === 1) {
  320. $admin_query->andWhere(['city_id' => $params['city_id'], 'district_id' => $params['district_id'], 'area_level' => 1]);
  321. }
  322. $admin = $admin_query->asArray()->one();
  323. if ($admin) {
  324. throw new \Exception('当前区域内已存在同等级代理');
  325. }
  326. }
  327. }
  328. if (Admin::findOne(['username' => $username, 'is_delete' => 0])) {
  329. throw new \Exception('用户名已经存在');
  330. }
  331. // if (!empty($saas_user->mobile)) {
  332. // if (Admin::findOne(['mobile' => $saas_user->mobile, 'is_delete' => 0, 'type' => $type])) {
  333. // throw new \Exception('手机号已经存在');
  334. // }
  335. // } else {
  336. // throw new \Exception('当前用户手机号为空');
  337. // }
  338. //产品代理自动选择区域代理
  339. if (in_array((int)$params['type'], [0, 2])) {
  340. if (empty($params['province_id']) || empty($params['city_id']) || empty($params['district_id'])) {
  341. throw new \Exception('参数不全');
  342. }
  343. if ((int)$params['type'] === 2) {
  344. $areaAdmin = Admin::find()->where(['AND', [
  345. "province_id" => $params['province_id'],
  346. "city_id" => $params['city_id'],
  347. "district_id" => $params['district_id'],
  348. 'area_level' => 1,
  349. 'is_delete' => 0,
  350. 'type' => 'admin',
  351. ], ['<>', 'username', 'admin']])->asArray()->one();
  352. if (empty($areaAdmin)) {
  353. throw new \Exception('当前区域下没有区域代理');
  354. }
  355. $params['area_level'] = $areaAdmin['area_level'];
  356. }
  357. if (empty($params['area_level'])) {
  358. throw new \Exception('请选择代理等级');
  359. }
  360. }
  361. $agent_apply = $apply_ ?: new AgentApply();
  362. if (isset($params['province_id']) && $params['province_id']) {
  363. $agent_apply->province_id = $params['province_id'];
  364. }
  365. if (isset($params['city_id']) && $params['city_id']) {
  366. $agent_apply->city_id = $params['city_id'];
  367. }
  368. if (isset($params['district_id']) && $params['district_id']) {
  369. $agent_apply->district_id = $params['district_id'];
  370. }
  371. if (isset($params['address']) && $params['address']) {
  372. $agent_apply->address = $params['address'];
  373. }
  374. if (isset($params['lat']) && $params['lat']) {
  375. $agent_apply->lat = $params['lat'];
  376. }
  377. if (isset($params['lng']) && $params['lng']) {
  378. $agent_apply->lng = $params['lng'];
  379. }
  380. $agent_apply->area_level = !empty($params['area_level']) ? $params['area_level'] : 0;
  381. $agent_apply->username = $username;
  382. if (strlen($saas_user->mobile) < 8) {
  383. throw new \Exception('手机号信息错误,请清除小程序重新授权');
  384. }
  385. $mobile = (string)$saas_user->mobile;
  386. $password = substr($mobile, -8, 8);
  387. $agent_apply->password = $password;
  388. $agent_apply->mobile = $mobile;
  389. $agent_apply->name = $saas_user->name;//((int)$params['type'] === 1 ? '推广代理-' : '区域代理-') . $saas_user->name;
  390. $agent_apply->saas_user_id = $saas_user->id;
  391. $agent_apply->agent_type = (int)$params['type'];
  392. $agent_apply->status = 0;
  393. if (!$agent_apply->save()) {
  394. throw new \Exception(json_encode($agent_apply->errors));
  395. }
  396. return $this->asJson([
  397. 'code' => 0,
  398. 'msg' => '申请成功, 请等待审核'
  399. ]);
  400. } catch (\Exception $e) {
  401. return $this->asJson([
  402. 'code' => 1,
  403. 'msg' => $e->getMessage(),
  404. ]);
  405. }
  406. }
  407. }