| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\controllers;
- use app\models\Admin;
- use app\models\Purchase;
- use app\models\SaasUser;
- use app\modules\admin\models\PlatformForm;
- use app\constants\OptionSetting;
- use app\models\StoreCloud;
- class PurchaseController extends BaseController
- {
- /**
- * 获取采购商列表
- * @return \yii\web\Response
- */
- public function actionGetList()
- {
- $name = get_params('name');
- $mobile = get_params('mobile');
- $status = get_params('status', -1);
- if ($status > 2) {
- $status = -1;
- }
- $query = Purchase::find()->alias('p')->where(['p.is_delete' => 0])
- ->leftJoin(['su' => SaasUser::tableName()], 'p.saas_user_id = su.id');
-
- if ($name) {
- $query->andWhere(['like', 'p.name', $name]);
- }
- if ($mobile) {
- $query->andWhere(['like', 'p.mobile', $mobile]);
- }
- if ($status > -1) {
- $query->andWhere(['p.status' => (int)$status]);
- }
- $admin = get_admin();
- $admin_id = $admin->id;
- if ($admin->username == 'admin') {
- $admin_id = null;
- }
- if ($admin_id) {
- $admin_model = Admin::findOne($admin_id);
- $area_level = $admin_model->area_level;
- if($area_level == 1){
- $query->andWhere(
- ['p.province_id' => $admin_model->province_id, 'p.city_id' => $admin_model->city_id, 'p.district_id' => $admin_model->district_id]
- );
- } elseif ($area_level == 2){
- $query->andWhere(
- ['p.province_id' => $admin_model->province_id, 'p.city_id' => $admin_model->city_id]
- );
- } elseif ($area_level == 3){
- $query->andWhere(['p.province_id' => $admin_model->province_id]);
- }
- }
- $query->select('p.*, su.name as nickname, su.avatar')->orderBy(['p.updated_at' => SORT_DESC]);
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- $item['custom_data'] = json_decode($item['custom_data']);
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $pagination,
- ]);
- }
- // 审核采购员
- public function actionApplyPurchase()
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $id = post_params('id');
- $status = post_params('status');
- if (!$id || !$status) {
- throw new \Exception('缺少必要参数');
- }
- $item = Purchase::findOne(['id' => $id, 'is_delete' => 0]);
- if (!$item) {
- throw new \Exception('数据未找到');
- }
- if ($status == 2) {
- $item->status = 2;
- $item->save();
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '操作成功',
- ]);
- }
- if ($status == 1) {
- //获取平台token
- $cloud_token = get_platform_token();
- if (empty($cloud_token)) {
- throw new \Exception('网络问题请重试');
- }
- $saas_user = SaasUser::findOne($item->saas_user_id);
- if (!$saas_user) {
- throw new \Exception('用户未找到');
- }
- $storeCloud = StoreCloud::findOne(['is_delete' => 0, 'store_id' => $item->store_id]);
- if ($storeCloud) {
- throw new \Exception('商城已经存在');
- }
- $storeCloud = StoreCloud::findOne(['is_delete' => 0, 'saas_user_id' => $item->saas_user_id]);
- if ($storeCloud) {
- throw new \Exception('用户已经绑定过云仓账户');
- }
- $domain = (new OptionSetting)->getCloudDomainName();
- $url = "/cloud/purchase/createPurchase";
- $data['access_token'] = $cloud_token;
- $data['name'] = $item->name;
- $data['logo'] = $saas_user->avatar;
- $data['tel'] = $item->mobile;
- $data['pwd'] = $item->mobile;
- $result = cloud_post($domain . $url, $data);
- $result = json_decode($result, true);
- if ((int)$result['code'] > 0 || !isset($result['code'])) {
- throw new \Exception('审核失败' . $result['msg']);
- }
- $mch_id = $result['data']['mch_id'];
- $user_id = $result['data']['user_id'];
- $storeCloud = new StoreCloud();
- $storeCloud->store_id = 0;
- $storeCloud->cloud_user_id = $user_id;
- $storeCloud->cloud_store_id = $mch_id;
- $storeCloud->saas_user_id = $item->saas_user_id;
- $storeCloud->name = $item->name;
- $storeCloud->password = $item->mobile;
- $storeCloud->logo = $saas_user->avatar;
- $storeCloud->type = 0;
- $storeCloud->tel = $item->mobile;
- $storeCloud->created_at = time();
- $storeCloud->store_id = $item->store_id;
- $storeCloud->province_id = $item->province_id;
- $storeCloud->city_id = $item->city_id;
- $storeCloud->district_id = $item->district_id;
- $storeCloud->save();
- $item->status = 1;
- $item->store_cloud_id = $storeCloud->id;
- $item->save();
- $t->commit();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '审核成功',
- ]);
- }
- throw new \Exception('非法参数');
- } catch (\Exception $e) {
- $t->rollBack();
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ]);
- }
- }
- // 删除采购商申请
- public function actionDeletePurchase()
- {
- $id = post_params('id');
- $item = Purchase::findOne(['id' => $id, 'is_delete' => 0]);
- if (!$item) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '数据未找到',
- ]);
- }
- $item->is_delete = 0;
- $item->save();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '删除成功',
- ]);
- }
- }
|