OrderForm.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1\alipay;
  8. use app\models\District;
  9. use app\models\FreeDeliveryRules;
  10. use app\models\Goods;
  11. use app\models\GoodsChainLevel;
  12. use app\models\Option;
  13. use app\models\Order;
  14. use app\models\OrderDetail;
  15. use app\models\PostageRules;
  16. use app\models\SaasUser;
  17. use app\models\ShareHolderLevel;
  18. use app\models\ShareHolderProfitGoodsLog;
  19. use app\models\StoreAliMini;
  20. use app\models\User;
  21. use app\modules\admin\models\AlipayThirdForm;
  22. use app\utils\Alipay\Alipay;
  23. use app\utils\Alipay\AlipayProfit;
  24. use app\utils\Alipay\alipaySdk\aop\request\AlipayUserInfoShareRequest;
  25. use app\utils\OrderNo;
  26. use yii\helpers\ArrayHelper;
  27. use yii\helpers\Json;
  28. class OrderForm extends ApiModel
  29. {
  30. public $store_id;
  31. public $mini_id;
  32. public $money;
  33. public $goods_id;
  34. public $attr;
  35. public $params;
  36. public $authCode;
  37. public $sourceId;
  38. public $user_id;
  39. public function rules()
  40. {
  41. return [
  42. [['money'], 'number'],
  43. [['params', 'sourceId', 'authCode'], 'string'],
  44. [['user_id', 'mini_id', 'store_id'], 'integer']
  45. ];
  46. }
  47. public function pay() {
  48. $res = AlipayProfit::mini(null, null, null);
  49. if ($res['code'] != 0) {
  50. return [
  51. 'code' => 1,
  52. 'msg' => $res['msg'],
  53. ];
  54. }
  55. return [
  56. 'code' => 0,
  57. 'msg' => 'success',
  58. 'data' => ['trade_no' => $res['data']['trade_no']]
  59. ];
  60. }
  61. //获取运费
  62. public function getFreight() {
  63. try {
  64. $params = json_decode($this->params ?? '', true);
  65. if (empty($params['addressInfo']['mobilePhone'])) {
  66. throw new \Exception('参数缺失');
  67. }
  68. $result = $this->getUserInfo($params['addressInfo']['mobilePhone'], $this->authCode, $this->store_id);
  69. if ($result['code']) {
  70. return [
  71. 'code' => 0,
  72. 'msg' => $result['msg'],
  73. 'data' => [
  74. 'freight' => 0,
  75. 'purchaseRestriction' => true,
  76. 'purchaseRestrictionCode' => 'OTHER'
  77. ]
  78. ];
  79. }
  80. if ($result['data']['user']['blacklist'] == User::USER_IN_BLACKLIST) {
  81. return [
  82. 'code' => 0,
  83. 'msg' => $result['msg'],
  84. 'data' => [
  85. 'freight' => 0,
  86. 'purchaseRestriction' => true,
  87. 'purchaseRestrictionCode' => 'BUYER_RISK'
  88. ]
  89. ];
  90. }
  91. $prov = $params['addressInfo']['prov'];
  92. $city = $params['addressInfo']['city'];
  93. $area = $params['addressInfo']['area'];
  94. $quantity = $params['orderDetailInfo']['quantity'];
  95. $salePrice = $params['itemInfo']['salePrice'];
  96. $outItemId = $params['itemInfo']['outItemId'];
  97. $outItemIdArr = explode('_', $outItemId);
  98. $goods = Goods::findOne(['id' => $outItemIdArr[1], 'store_id' => $this->store_id]);
  99. if (!$goods) {
  100. return [
  101. 'code' => 0,
  102. 'msg' => '商品不存在',
  103. 'data' => [
  104. 'freight' => 0,
  105. 'purchaseRestriction' => true,
  106. 'purchaseRestrictionCode' => 'ITEM_CANNOT_FOUND'
  107. ]
  108. ];
  109. }
  110. if (!intval($goods->status)) {
  111. return [
  112. 'code' => 0,
  113. 'msg' => '商品已下架',
  114. 'data' => [
  115. 'freight' => 0,
  116. 'purchaseRestriction' => true,
  117. 'purchaseRestrictionCode' => 'ITEM_OFFLINE'
  118. ]
  119. ];
  120. }
  121. if (!intval($goods->goods_num)) {
  122. return [
  123. 'code' => 0,
  124. 'msg' => '商品已售完',
  125. 'data' => [
  126. 'freight' => 0,
  127. 'purchaseRestriction' => true,
  128. 'purchaseRestrictionCode' => 'ITEM_SELLOUT'
  129. ]
  130. ];
  131. }
  132. Goods::skuAttr($goods);
  133. $price_open = false;
  134. $attr_open = false;
  135. $attr = json_decode($goods->attr, true);//$params['itemInfo']['outSkuId'];
  136. foreach ($attr as $attr_item) {
  137. if ($attr_item['cyy_skuId'] === $params['itemInfo']['outSkuId']) {
  138. $attr_open = true;
  139. if (floor_num($salePrice, 2) !== floor_num($attr_item['price'], 2)) {
  140. $price_open = true;
  141. }
  142. }
  143. }
  144. if ($price_open || !$attr_open) {
  145. return [
  146. 'code' => 0,
  147. 'msg' => '价格不对应',
  148. 'data' => [
  149. 'freight' => 0,
  150. 'purchaseRestriction' => true,
  151. 'purchaseRestrictionCode' => 'ITEM_SKU_NOT_MATCH'
  152. ]
  153. ];
  154. }
  155. $mch = [
  156. 'goods_list' => [
  157. [
  158. 'goods_id' => $goods->id,
  159. 'freight' => $goods->freight,
  160. 'num' => $quantity,
  161. 'weight' => $goods->weight,
  162. 'price' => $salePrice,
  163. ]
  164. ],
  165. 'total_price' => bcmul($salePrice, $quantity, 2)
  166. ];
  167. $province = District::find()->where(['LIKE', 'name', $prov])->asArray()->one();
  168. $city = District::find()->where(['LIKE', 'name', $city])->asArray()->one();
  169. $area = District::find()->where(['LIKE', 'name', $area])->asArray()->one();
  170. if (empty($province) || empty($city) || empty($area)) {
  171. throw new \Exception('区域错误');
  172. }
  173. $address = [
  174. 'province_id' => $province['id'],
  175. 'city_id' => $city['id'],
  176. 'district_id' => $area['id']
  177. ];
  178. $err_data = [];
  179. $expressPrice = 0;
  180. if ($address) {
  181. //先计算单品满件包邮和满额包邮
  182. $resGoodsList = Goods::cutFull($mch['goods_list']);
  183. //再通过运费规则计算运费
  184. $expressPrice = PostageRules::getExpressPriceMore($this->store_id ?? get_store_id(), $address['city_id'], $resGoodsList, $address['province_id'], $err_data);
  185. }
  186. $mchTotalPrice = array_sum(array_column($mch['goods_list'], 'price'));
  187. $expressPrice = $this->getFreeDeliveryRules($mch, $expressPrice, $mchTotalPrice, $address);
  188. return [
  189. 'code' => 0,
  190. 'msg' => '',
  191. 'data' => [
  192. 'freight' => $expressPrice['expressPrice'] >= 0 ? $expressPrice['expressPrice'] : 0,
  193. 'purchaseRestriction' => !empty($err_data),
  194. 'purchaseRestrictionCode' => !empty($err_data) ? 'OTHER' : '',
  195. 'user_id' => $result['data']['user']['id']
  196. ]
  197. ];
  198. } catch (\Exception $e) {
  199. return [
  200. 'code' => 0,
  201. 'msg' => $e->getMessage(),
  202. 'data' => [
  203. 'freight' => 0,
  204. 'purchaseRestriction' => true,
  205. 'purchaseRestrictionCode' => 'OTHER'
  206. ]
  207. ];
  208. }
  209. }
  210. //创建订单信息
  211. public function createOrder() {
  212. try {
  213. $sourceId = $this->sourceId;
  214. $user_id = $this->user_id;
  215. $store_id = $this->store_id;
  216. $params = json_decode($this->params, true);
  217. $prov = $params['addressInfo']['prov'];
  218. $city = $params['addressInfo']['city'];
  219. $area = $params['addressInfo']['area'];
  220. $address_detail = $params['addressInfo']['address'];
  221. $province = District::find()->where(['LIKE', 'name', $prov])->asArray()->one();
  222. $city = District::find()->where(['LIKE', 'name', $city])->asArray()->one();
  223. $area = District::find()->where(['LIKE', 'name', $area])->asArray()->one();
  224. $express_price = $params['priceInfo']['freight'];
  225. $quantity = $params['orderInfo']['quantity'];
  226. $salePrice = $params['itemInfo']['salePrice'];
  227. $outItemId = $params['itemInfo']['outItemId'];
  228. $outItemIdArr = explode('_', $outItemId);
  229. $goods = Goods::findOne(['id' => $outItemIdArr[1], 'store_id' => $this->store_id]);
  230. if (!$goods) {
  231. throw new \Exception('商品不存在');
  232. }
  233. Goods::skuAttr($goods);
  234. $goods_attr = [];
  235. $attr = json_decode($goods->attr, true);//$params['itemInfo']['outSkuId'];
  236. foreach ($attr as $attr_item) {
  237. if ($attr_item['cyy_skuId'] === $params['itemInfo']['outSkuId']) {
  238. $goods_attr = $attr_item['attr_list'];
  239. if (floor_num($salePrice, 2) !== floor_num($attr_item['price'], 2)) {
  240. $cost_price = $attr_item['cost_price'];
  241. }
  242. }
  243. }
  244. $t = \Yii::$app->db->beginTransaction();
  245. try {
  246. $order = new Order();
  247. $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_MALL);
  248. $order->order_origin = Order::ORDER_SOURCE_MINI;
  249. $order->store_id = $store_id;
  250. $order->user_id = $user_id;
  251. $order->name = $params['addressInfo']['fullname'];
  252. $order->mobile = $params['addressInfo']['mobilePhone'];
  253. $order->address = $province['name'] ?? '' . $city['name'] ?? '' . $area['name'] ?? '' . $address_detail;
  254. $order->province_id = $province['id'] ?: 0;
  255. $order->city_id = $city['id'] ?: 0;
  256. $order->district_id = $area['id'] ?: 0;
  257. $order->town_id = 0;
  258. $order->village_id = 0;
  259. $order->address_data = json_encode([
  260. 'province' => trim($province['name']),
  261. 'city' => trim($city['name']),
  262. 'district' => trim($area['name']),
  263. 'detail' => trim($address_detail),
  264. 'town' => '',
  265. 'village' => '',
  266. 'latitude' => 0,
  267. 'longitude' => 0,
  268. ], JSON_UNESCAPED_UNICODE);
  269. $order->buy_level_id = 0;
  270. $order->total_price = $order->pay_price = bcadd(bcmul($salePrice, $quantity, 2), $express_price, 2);
  271. $order->express_price = $express_price;
  272. $order->md_id = 0;
  273. $order->is_fugou = 0;
  274. $order->created_at = time();
  275. $order->order_type = 0;
  276. $order->integral_price = 0;
  277. $order->integral_difference_price = 0;
  278. $order->is_platform = 0;
  279. $order->first_price = 0.00;
  280. $order->second_price = 0.00;
  281. $order->third_price = 0.00;
  282. $order->transaction_id = '';
  283. $order->integral = json_encode(['forehead' => 0, 'forehead_integral' => 0]);
  284. // $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_MALL);
  285. $order->is_pay = 0;
  286. $order->version = cyy_version();
  287. if (!$order->save()) {
  288. throw new \Exception(json_encode($order->errors));
  289. }
  290. $order_detail = new OrderDetail();
  291. $order_detail->order_id = $order->id;
  292. $order_detail->goods_id = $goods->id;
  293. $order_detail->num = $quantity;
  294. $order_detail->cost_price = $cost_price;
  295. $order_detail->total_price = bcmul($salePrice, $quantity, 2);
  296. // 计算单个商品可分红金额
  297. $profit = $goods->rate_type == 0 ? floatval($order_detail->total_price * $goods->rate / 100) : floatval($goods->rate) * $quantity;
  298. $goods_profit = $profit;
  299. $order_detail->profit = $profit;
  300. // 链动
  301. $chain_profit = $goods->chain_rate_type == 0 ? floatval($order_detail->total_price * $goods->chain_rate / 100) : floatval($goods->chain_rate) * $quantity;
  302. $chain_goods_profit = $chain_profit;
  303. $order_detail->chain_profit = $chain_profit;
  304. $order_detail->is_delete = 0;
  305. $chainGoodsList[] = [
  306. 'id' => $goods->id,
  307. 'num' => $quantity,
  308. 'pay_price' => $order_detail->total_price,
  309. ];
  310. $shareHolderRepeatProfitSwitch = (int)Option::get('shareHolderRepeatProfitSwitch', $order->store_id, 'bonus_pool', 0)['value'];
  311. //判断是否是升级产品
  312. $open = false;
  313. $shareHolderLevelList = ShareHolderLevel::find()->where(['store_id' => $order->store_id, 'status' => 1, 'is_delete' => 0])
  314. ->select('condition, id, level, member_level')->asArray()->all();
  315. foreach ($shareHolderLevelList as $item) {
  316. $condition = json_decode($item['condition'], true);
  317. if ((int)$condition['goods']['is_open'] === 1 && !empty($condition['goods']['value']['id']) && in_array($goods->id, $condition['goods']['value']['id'])) {
  318. $open = true;
  319. break;
  320. }
  321. }
  322. //如果是分红产品或者是升级产品则添加记录
  323. if (floatval(sprintf("%.2f", ($profit / $quantity))) > 0 || $open) {
  324. if ($shareHolderRepeatProfitSwitch) {
  325. $profitGoodsLog = new ShareHolderProfitGoodsLog();
  326. $profitGoodsLog->user_id = get_user_id();
  327. $profitGoodsLog->goods_id = $goods->id;
  328. $profitGoodsLog->order_id = $order->id;
  329. $profitGoodsLog->goods_price = floatval(sprintf("%.2f", ($order_detail->total_price / $quantity)));
  330. $profitGoodsLog->goods_profit = floatval(sprintf("%.2f", ($profit / $quantity)));
  331. $profitGoodsLog->is_switch = intval($shareHolderRepeatProfitSwitch);
  332. if (!$profitGoodsLog->save()) {
  333. return [
  334. 'code' => 1,
  335. 'msg' => implode(';', array_values($profitGoodsLog->firstErrors))
  336. ];
  337. };
  338. // }
  339. }
  340. }
  341. //
  342. $order_detail->attr = json_encode($goods_attr, JSON_UNESCAPED_UNICODE);
  343. $order_detail->food_ext_goods = '';
  344. $order_detail->pic = $goods->cover_pic;
  345. $order_detail->goods_name = $goods->name;
  346. if (!empty($goods)) {
  347. //在添加下单商品信息时增加链动等级记录 为了实现小推大功能
  348. $goods = $goods->toArray();
  349. $goods['goods_chain_level'] = GoodsChainLevel::find()->where(['goods_id' => $goods['id']])->asArray()->one();
  350. }
  351. $order_detail->goods_info = $goods ? Json::encode($goods) : Json::encode([]);
  352. $order_detail->delivery_type = OrderDetail::GOODS_DELIVERY_EXPRESS;
  353. $order_detail->integral = 0;
  354. $attr_id_list = [];
  355. foreach ($goods_attr as $item) {
  356. array_push($attr_id_list, $item['attr_id']);
  357. }
  358. $order_detail->batch_price_tips = $goods['current_batch_price_tips'];
  359. if (!$order_detail->save()) {
  360. throw new \Exception('订单详情信息错误');
  361. }
  362. // goods_profit 计算之后存入订单表中
  363. $order->profit = $goods_profit;
  364. $order->chain_profit = $chain_goods_profit;
  365. $orderChainLevelProfit = \app\utils\Share\BonusPool::getOrderLevelProfit(get_store_id(), $chainGoodsList);
  366. $order->chain_level_value = json_encode($orderChainLevelProfit);
  367. if (!$order->save()) {
  368. throw new \Exception('订单信息错误');
  369. }
  370. $t->commit();
  371. } catch (\Exception $e) {
  372. $t->rollBack();
  373. throw new \Exception($e->getMessage());
  374. }
  375. $goods_detail = [
  376. [
  377. 'goods_id' => $outItemIdArr[1],
  378. 'goods_name' => $order_detail->goods_name,
  379. 'quantity' => $order_detail->num,
  380. 'price' => $order_detail->total_price / $order_detail->num,
  381. ]
  382. ];
  383. $user = User::findOne($user_id);
  384. $goods_names = mb_substr($order_detail->goods_name . ';', 0, 32, 'utf-8');
  385. $orderInfo = [
  386. 'body' => $goods_names,
  387. 'out_trade_no' => $order->order_no,
  388. 'total_amount' => $order->pay_price,
  389. 'subject' => $goods_names,
  390. 'buyer_id' => $user->alipay_open_id
  391. ];
  392. $goods_detail && $orderInfo['goods_detail'] = $goods_detail;
  393. $alipayForm = new AlipayThirdForm();
  394. $alipayForm->mini_id = $this->mini_id;
  395. $openOrder = $alipayForm->AlipayOpenMiniOrderCreate($order, $orderInfo, (object)[
  396. 'ali_sourceId' => $sourceId,
  397. 'activity_consult_id' => $params['orderInfo']['activityConsultId']
  398. ]);
  399. if($openOrder['code'] != 0){
  400. return $openOrder;
  401. }
  402. $orderInfo['product_code'] = 'JSAPI_PAY';
  403. $orderInfo['extend_params'] = array_merge((array)$orderInfo['extend_params'], [
  404. 'trade_component_order_id' => $openOrder['data']
  405. ]);
  406. $orderInfo['goods_detail'] = [];
  407. foreach($openOrder['biz_content']['order_detail']['item_infos'] as $info_item){
  408. $orderInfo['goods_detail'][] = [
  409. 'goods_id' => $info_item['goods_id'],
  410. 'goods_name' => $info_item['goods_name'],
  411. 'quantity' => $info_item['item_cnt'],
  412. 'price' => $info_item['sale_price'],
  413. 'out_item_id' => $info_item['out_item_id'],
  414. 'out_sku_id' => $info_item['out_sku_id']
  415. ];
  416. }
  417. if (is_profit_pay('ali')) {
  418. AlipayProfit::init($order->store_id, '');
  419. $result = AlipayProfit::$alipay->mini($orderInfo);
  420. } else {
  421. Alipay::init('');
  422. $result = Alipay::$alipay->mini($orderInfo);
  423. }
  424. if ($result && $result->code === '10000' && $result->msg === 'Success') {
  425. return [
  426. 'code' => 0,
  427. 'msg' => 'success',
  428. 'data' => $result->toArray()
  429. ];
  430. }
  431. throw new \Exception($result->msg);
  432. } catch (\Exception $e) {
  433. return [
  434. 'code' => 1,
  435. 'msg' => $e->getMessage()
  436. ];
  437. }
  438. }
  439. //获取运费规则
  440. public function getFreeDeliveryRules($mch, $expressPrice, $mchTotalPrice, $address) {
  441. if ($expressPrice == 0) {
  442. // return $expressPrice;
  443. return ['expressPrice' => $expressPrice, 'diff' => 0];
  444. }
  445. $freeDeliveryPrice = 0;
  446. $free = FreeDeliveryRules::find()->where(['store_id' => $this->store_id ?? get_store_id(), 'is_delete' => 0, 'mch_id' => 0])->asArray()->all();
  447. foreach ($free as $k => $v) {
  448. $city = json_decode($v['city'], true);
  449. foreach ($city as $v1) {
  450. if ($address['district_id'] == $v1['id'] && $mch['total_price'] >= $v['price']) {
  451. $expressPrice = 0;
  452. break;
  453. }
  454. if ($address['district_id'] == $v1['id']) {
  455. $freeDeliveryPrice = max($freeDeliveryPrice, $v['price']);
  456. }
  457. }
  458. }
  459. $diff = 0;
  460. // echo $mchTotalPrice;die;
  461. if ($freeDeliveryPrice > 0) {
  462. $diff = $freeDeliveryPrice - $mchTotalPrice;
  463. }
  464. if ($expressPrice > 0) {
  465. return ['expressPrice' => $expressPrice, 'diff' => $diff];
  466. } else {
  467. return ['expressPrice' => $expressPrice, 'diff' => 0];
  468. }
  469. }
  470. //创建/获取用户
  471. public function getUserInfo($mobile, $authCode, $store_id) {
  472. //查询用户是否存在 不存在就创建一个用户
  473. $open = false;
  474. $saas_user = SaasUser::findOne(['mobile' => $mobile, 'is_delete' => 0]);
  475. if (!$saas_user) {
  476. $open = true;
  477. }
  478. $user = User::findOne(['binding' => $mobile, 'is_delete' => 0, 'store_id' => $store_id]);
  479. if (!$user) {
  480. $open = true;
  481. }
  482. if ($open) {
  483. $storeAliMini = StoreAliMini::findOne(['store_id' => $store_id, 'is_cancel' => 0, 'is_use' => 1]);
  484. $auth_form = new AuthForm();
  485. $auth_form->mini_id = $storeAliMini->id;
  486. $auth_form->store_id = $store_id;
  487. [$token, $aop, $auth_token] = $auth_form->getToken($authCode);
  488. $auth_time = (strtotime($token['auth_start']) + $token['expires_in']);
  489. if ($auth_time < time()) {
  490. [$token, $aop, $auth_token] = $auth_form->getToken($authCode, 1, $token['refresh_token']);
  491. }
  492. // $request = new AlipayUserInfoShareRequest();
  493. //
  494. // $result = $aop->execute($request, $token['access_token'], $auth_token);
  495. // $result = json_encode($result);
  496. // $result = json_decode($result,true);
  497. // \Yii::error($result);
  498. // $token = $result['alipay_user_info_share_response'];
  499. // if ((int)$token['code'] !== 10000) {
  500. // return [
  501. // 'code' => 1,
  502. // 'msg' => $token['msg']
  503. // ];
  504. // }
  505. if (!$saas_user) {
  506. $saas_user = new SaasUser();
  507. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  508. $saas_user->avatar = $token['avatar'] ?: (\Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png');
  509. $saas_user->mobile = $mobile ?? '';
  510. $saas_user->name = $token['nick_name'] ?: '';
  511. $saas_user->store_id = $store_id;
  512. $saas_user->ali_user_id = $token['user_id'] ?: '';
  513. if (!$saas_user->save()) {
  514. return [
  515. 'code' => 1,
  516. 'msg' => '获取失败',
  517. ];
  518. }
  519. }
  520. if (!$user) {
  521. $default_user_info_arr = OptionSetting::default_user_info_arr();
  522. $default_user_info = [
  523. 'default_user_nickname' => $default_user_info_arr['default_user_nickname'] ?? '',
  524. 'default_user_pic' => $default_user_info_arr['default_user_pic'] ?? '',
  525. ];
  526. $user = new User();
  527. $user->type = User::USER_TYPE_NORMAL;
  528. $user->binding = $mobile ?? '';
  529. $user->avatar_url = $token['avatar'] ?: ($default_user_info['default_user_pic'] ?: \Yii::$app->request->hostInfo . '/web/v1/statics/images/avatar.png');
  530. $user->username = \Yii::$app->security->generateRandomString();
  531. $user->nickname = $token['nick_name'] ?: $default_user_info['default_user_nickname'];
  532. $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
  533. $user->auth_key = \Yii::$app->security->generateRandomString();
  534. $user->access_token = \Yii::$app->security->generateRandomString();
  535. $user->is_delete = User::USER_NOT_DELETE;
  536. $user->alipay_open_id = $token['user_id'] ?: '';
  537. $user->store_id = $store_id;
  538. $user->platform = User::USER_FROM_ALIPAY; // 支付宝
  539. if (!$user->save()) {
  540. return [
  541. 'code' => 1,
  542. 'msg' => '获取失败',
  543. ];
  544. }
  545. }
  546. }
  547. return [
  548. 'code' => 0,
  549. 'msg' => '操作成功',
  550. 'data' => [
  551. 'user' => ArrayHelper::toArray($user)
  552. ]
  553. ];
  554. }
  555. }