PurchaseController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers;
  8. use app\models\Admin;
  9. use app\models\Purchase;
  10. use app\models\SaasUser;
  11. use app\modules\admin\models\PlatformForm;
  12. use app\constants\OptionSetting;
  13. use app\models\StoreCloud;
  14. class PurchaseController extends BaseController
  15. {
  16. /**
  17. * 获取采购商列表
  18. * @return \yii\web\Response
  19. */
  20. public function actionGetList()
  21. {
  22. $name = get_params('name');
  23. $mobile = get_params('mobile');
  24. $status = get_params('status', -1);
  25. if ($status > 2) {
  26. $status = -1;
  27. }
  28. $query = Purchase::find()->alias('p')->where(['p.is_delete' => 0])
  29. ->leftJoin(['su' => SaasUser::tableName()], 'p.saas_user_id = su.id');
  30. if ($name) {
  31. $query->andWhere(['like', 'p.name', $name]);
  32. }
  33. if ($mobile) {
  34. $query->andWhere(['like', 'p.mobile', $mobile]);
  35. }
  36. if ($status > -1) {
  37. $query->andWhere(['p.status' => (int)$status]);
  38. }
  39. $admin = get_admin();
  40. $admin_id = $admin->id;
  41. if ($admin->username == 'admin') {
  42. $admin_id = null;
  43. }
  44. if ($admin_id) {
  45. $admin_model = Admin::findOne($admin_id);
  46. $area_level = $admin_model->area_level;
  47. if($area_level == 1){
  48. $query->andWhere(
  49. ['p.province_id' => $admin_model->province_id, 'p.city_id' => $admin_model->city_id, 'p.district_id' => $admin_model->district_id]
  50. );
  51. } elseif ($area_level == 2){
  52. $query->andWhere(
  53. ['p.province_id' => $admin_model->province_id, 'p.city_id' => $admin_model->city_id]
  54. );
  55. } elseif ($area_level == 3){
  56. $query->andWhere(['p.province_id' => $admin_model->province_id]);
  57. }
  58. }
  59. $query->select('p.*, su.name as nickname, su.avatar')->orderBy(['p.updated_at' => SORT_DESC]);
  60. $pagination = pagination_make($query);
  61. foreach ($pagination['list'] as &$item) {
  62. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  63. $item['custom_data'] = json_decode($item['custom_data']);
  64. }
  65. return $this->asJson([
  66. 'code' => 0,
  67. 'msg' => 'success',
  68. 'data' => $pagination,
  69. ]);
  70. }
  71. // 审核采购员
  72. public function actionApplyPurchase()
  73. {
  74. $t = \Yii::$app->db->beginTransaction();
  75. try {
  76. $id = post_params('id');
  77. $status = post_params('status');
  78. if (!$id || !$status) {
  79. throw new \Exception('缺少必要参数');
  80. }
  81. $item = Purchase::findOne(['id' => $id, 'is_delete' => 0]);
  82. if (!$item) {
  83. throw new \Exception('数据未找到');
  84. }
  85. if ($status == 2) {
  86. $item->status = 2;
  87. $item->save();
  88. $t->commit();
  89. return $this->asJson([
  90. 'code' => 0,
  91. 'msg' => '操作成功',
  92. ]);
  93. }
  94. if ($status == 1) {
  95. //获取平台token
  96. $cloud_token = get_platform_token();
  97. if (empty($cloud_token)) {
  98. throw new \Exception('网络问题请重试');
  99. }
  100. $saas_user = SaasUser::findOne($item->saas_user_id);
  101. if (!$saas_user) {
  102. throw new \Exception('用户未找到');
  103. }
  104. $storeCloud = StoreCloud::findOne(['is_delete' => 0, 'store_id' => $item->store_id]);
  105. if ($storeCloud) {
  106. throw new \Exception('商城已经存在');
  107. }
  108. $storeCloud = StoreCloud::findOne(['is_delete' => 0, 'saas_user_id' => $item->saas_user_id]);
  109. if ($storeCloud) {
  110. throw new \Exception('用户已经绑定过云仓账户');
  111. }
  112. $domain = (new OptionSetting)->getCloudDomainName();
  113. $url = "/cloud/purchase/createPurchase";
  114. $data['access_token'] = $cloud_token;
  115. $data['name'] = $item->name;
  116. $data['logo'] = $saas_user->avatar;
  117. $data['tel'] = $item->mobile;
  118. $data['pwd'] = $item->mobile;
  119. $result = cloud_post($domain . $url, $data);
  120. $result = json_decode($result, true);
  121. if ((int)$result['code'] > 0 || !isset($result['code'])) {
  122. throw new \Exception('审核失败' . $result['msg']);
  123. }
  124. $mch_id = $result['data']['mch_id'];
  125. $user_id = $result['data']['user_id'];
  126. $storeCloud = new StoreCloud();
  127. $storeCloud->store_id = 0;
  128. $storeCloud->cloud_user_id = $user_id;
  129. $storeCloud->cloud_store_id = $mch_id;
  130. $storeCloud->saas_user_id = $item->saas_user_id;
  131. $storeCloud->name = $item->name;
  132. $storeCloud->password = $item->mobile;
  133. $storeCloud->logo = $saas_user->avatar;
  134. $storeCloud->type = 0;
  135. $storeCloud->tel = $item->mobile;
  136. $storeCloud->created_at = time();
  137. $storeCloud->store_id = $item->store_id;
  138. $storeCloud->province_id = $item->province_id;
  139. $storeCloud->city_id = $item->city_id;
  140. $storeCloud->district_id = $item->district_id;
  141. $storeCloud->save();
  142. $item->status = 1;
  143. $item->store_cloud_id = $storeCloud->id;
  144. $item->save();
  145. $t->commit();
  146. return $this->asJson([
  147. 'code' => 0,
  148. 'msg' => '审核成功',
  149. ]);
  150. }
  151. throw new \Exception('非法参数');
  152. } catch (\Exception $e) {
  153. $t->rollBack();
  154. return $this->asJson([
  155. 'code' => 1,
  156. 'msg' => $e->getMessage(),
  157. ]);
  158. }
  159. }
  160. // 删除采购商申请
  161. public function actionDeletePurchase()
  162. {
  163. $id = post_params('id');
  164. $item = Purchase::findOne(['id' => $id, 'is_delete' => 0]);
  165. if (!$item) {
  166. return $this->asJson([
  167. 'code' => 1,
  168. 'msg' => '数据未找到',
  169. ]);
  170. }
  171. $item->is_delete = 0;
  172. $item->save();
  173. return $this->asJson([
  174. 'code' => 0,
  175. 'msg' => '删除成功',
  176. ]);
  177. }
  178. }