| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace app\modules\admin\models;
- use app\constants\OptionSetting;
- use app\models\Admin;
- use app\models\SaasCategory;
- use app\models\Store;
- use app\models\StoreCloud;
- use app\models\StoreForbiddenDirectory;
- use yii\base\Model;
- class StoreForbiddenDirectoryForm extends Model
- {
- public $store_name;
- public $store_id;
- public $id;
- public $cloud_cat_id;
- public $category_id;
- public function rules()
- {
- return [
- [['store_name'], 'string'],
- [['id', 'category_id'], 'integer'],
- [['cloud_cat_id', 'store_id'], 'safe']
- ];
- }
- //店铺禁售目录
- public function storeForbiddenDirectory()
- {
- $store_name = $this->store_name;
- $category_id = $this->category_id;
- $query = Store::find()->alias('s')
- ->leftJoin(['a' => Admin::tableName()], 'a.type_id = s.id AND a.type = "store"')
- ->rightJoin(['sfd' => StoreForbiddenDirectory::tableName()], 'sfd.store_id = s.id');
- $query->where(['s.is_delete' => 0, 'sfd.is_delete' => 0]);
- if ($store_name) {
- $query->andWhere(['like', 's.name', $store_name]);
- }
- if ($category_id) {
- $query->andWhere(['s.category_id' => $category_id]);
- }
- $query->select('s.id, s.name as store_name, s.logo, a.name as nickname, a.mobile, sfd.cloud_cat_id, s.category_id, sfd.cloud_cat_name');
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- $item['cat_name'] = '-';
- if ($item['category_id'] > 0) {
- $cat = SaasCategory::findOne($item['category_id']);
- if ($cat) {
- $item['cat_name'] = $cat->name;
- }
- }
- if ($item['cloud_cat_id']) {
- $item['cloud_cat_id'] = explode(',', $item['cloud_cat_id']);
- } else {
- $item['cloud_cat_id'] = [];
- }
- }
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $list
- ];
- }
- //保存
- public function save()
- {
- try {
- $store_id = $this->store_id;
- $cloud_cat_id = $this->cloud_cat_id;
- //判断是否拥有云仓权限
- $merchant_token = get_merchant_token(0, $store_id);
- if (empty($merchant_token)) {
- throw new \Exception('该店铺未拥有云仓权限');
- }
- if (empty($cloud_cat_id)) {
- throw new \Exception('分类不能为空');
- }
- $cloud_token = get_platform_token();
- $store_user_add_url = "/cloud/user/getGoodsCatList";
- $store_user_add_data['access_token'] = $cloud_token;
- $store_user_add_data['ids'] = implode(',', $cloud_cat_id);
- $cat_list_info = cloud_post((new OptionSetting)->getCloudDomainName().$store_user_add_url,$store_user_add_data);
- $cat_list_result = json_decode($cat_list_info,true);
- if(empty($cat_list_result) || $cat_list_result['code'] != 0){
- return $cat_list_result;
- }
- $cat_list = $cat_list_result['data']['cat_list'];
- $cat_name = $id = [];
- if (!empty($cat_list)) {
- $cat_name = array_column($cat_list, 'name');
- $id = array_column($cat_list, 'id');
- }
- $store_forbidden_directory = StoreForbiddenDirectory::findOne(['store_id' => $store_id, 'is_delete' => 0]);
- if (empty($store_forbidden_directory)) {
- $store_forbidden_directory = new StoreForbiddenDirectory();
- $store_forbidden_directory->store_id = $store_id;
- }
- $store_forbidden_directory->cloud_cat_id = implode(',', $id);
- $store_forbidden_directory->cloud_cat_name = implode('/', $cat_name);
- if (!$store_forbidden_directory->save()) {
- throw new \Exception(json_encode($store_forbidden_directory->errors, JSON_UNESCAPED_UNICODE));
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //删除
- public function del()
- {
- try {
- $store_id = explode(',', $this->store_id);
- foreach ($store_id as $item) {
- $storeForbiddenDirectory = StoreForbiddenDirectory::findOne(['store_id' => $store_id, 'is_delete' => 0]);
- $storeForbiddenDirectory->is_delete = 1;
- $storeForbiddenDirectory->save();
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|